문제

I have an app where the user can choose to share an update with people via Twitter and Facebook, if they wish to share via both then things are tricky. If you try and call their SLComposeViewController one after the other only Twitter appears and Facebook never appears at all. I have tried to get around this by using NSNotifications but they never seem to be called, and by using completion handlers but those never seem to work and just make the whole UI go bizarre. Can anyone help me on how I would go about displaying am SLComposeView one after the other? I've been banging my head against the wall for four hours.

도움이 되었습니까?

해결책

Assuming that you were trying to use presentViewController's completion handler and getting incorrect results, here's an alternative way. You can present the first composer as you normally would, but then in the composer's completion block, present a second composer. This completion handler is called as soon as the first composer is done being dismissed.

In the example I've set up below, the second composer will only be presented if the first returns SLComposeViewControllerResultDone, allowing you to close out all together in the event that the user hits cancel. However, if you don't want this functionality it can be removed painlessly by keeping the logic for presenting the second composer, but removing the switch statement all together. This code is tested and should produce the results you're looking for. Hope it helps!

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
    SLComposeViewController *facebookComposer = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    [facebookComposer setInitialText:@"facebook"];
    [facebookComposer setCompletionHandler:^(SLComposeViewControllerResult result) {

        switch (result) {
            case SLComposeViewControllerResultCancelled:
                NSLog(@"Post Canceled, bail out");
                break;
            case SLComposeViewControllerResultDone:

                if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
                    SLComposeViewController *twitterComposer = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
                    [twitterComposer setInitialText:@"twitter"];
                    [twitterComposer setCompletionHandler:^(SLComposeViewControllerResult result) {
                        NSLog(@"done with both sharing options");
                    }];
                    [self presentViewController:twitterComposer animated:YES completion:nil];
                }

                break;

            default:
                break;
        }
    }];
    [self presentViewController:facebookComposer animated:YES completion:nil];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top