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.
1 Answers
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];
}

- 129,200
- 40
- 280
- 281
-
That is pretty close to what I've been trying except for I am trying to call a method with the twitterComposer stuff in it using a __weak reference to self. Seems the easiest way is just going to be to duplicate that code within my first method as above and if the user only wants to post to the other to call the other method which will not use the completion handler. Thank you for the help :) – KelticKoder Jan 05 '13 at 23:20
-
@user1861474 Could you give a code example of what you're trying to do? I may be able to tweak it! And any time! If my answer helped don't forget to mark as correct :) – Mick MacCallum Jan 05 '13 at 23:24
-
In my app when a user adds an entry they can also share it with friends, they do this by toggling UISwitches (or leaving them) which indicates which services they wish to post to. If they need to post to both then I need a way to manage that. I was using the above and trying to make it call a separate method because it would make the code a little more efficient, but now I am using the above and three separate methods. Seems that the above example also breaks if you switch the services around to make Twitter show first. This is an SLSocial framework issue which I can't fully explain. Thanks :) – KelticKoder Jan 06 '13 at 00:35