0

I am presenting an SLComposeViewController to post to Facebook in my app. The user is able to dismiss this View Controller in one of two ways: either by posting their post to Facebook, or by pressing "cancel". When the user presses "cancel", the SLComposeViewController is dismissed, and the user is returned to the presenting View Controller that is behind it.

However, what I would like to do is if the user presses "post", then I want the presenting View Controller to ALSO be dismissed after the SLComposeViewController is dismissed (i.e. in the SLComposeViewControllerResultDone case). My problem is that I am not sure how to do this. I realize that I would use the completion handler for this, but I am stuck here. Here is the code that I have which presents the SLComposeViewController:

    SLComposeViewController *fbSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    [fbSheet setInitialText:initialText];
    [fbSheet addImage:myImage];

    SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result) {

    switch(result){
        case SLComposeViewControllerResultCancelled:
        default:
        {
            NSLog(@"Cancelled.....");

        }
            break;
        case SLComposeViewControllerResultDone:
        {
            NSLog(@"Posted....");
        }
            break;
    }

};

[fbSheet setCompletionHandler:completionHandler];

[self presentViewController:fbSheet animated:YES completion:nil];

With the completion handler above, I get the NSLog outputs as expected. However,

Can anyone see what it is I'm doing wrong? As I've pointed out, I need the dismissal of the presenting View Controller to occur ONLY if the user "posts" to Facebook, but NOT when they cancel.

halfer
  • 19,824
  • 17
  • 99
  • 186
syedfa
  • 2,801
  • 1
  • 41
  • 74
  • 1
    You could use the completionHandler of SLComposeViewController to know when the user has completedComposing the post. Depending upon result variable, you could dismiss you viewController. For More [Check this](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Reference/SLComposeViewController_Class/#//apple_ref/occ/instp/SLComposeViewController/completionHandler) – dispatchMain Dec 27 '14 at 06:43
  • @Ad-J Thanks very much for your comment. I've updated my code above to include your suggestion. However, I am not sure how to dismiss the calling viewController (i.e. the one that calls SLComposeViewController) once the user has successfully posted to Facebook. – syedfa Dec 27 '14 at 20:36
  • Check my answer below. It works fine with your provided code. – dispatchMain Dec 28 '14 at 09:21

2 Answers2

3

You could simply dismiss the presenting view controller in SLComposeViewControllerResultDone part of completionHandler as below:

    dispatch_async(dispatch_get_main_queue(), ^{
        [self dismissViewControllerAnimated:YES completion:nil];
    });

If you are supporting iOS 6, then you need to dismiss the SLComposeViewController first.

dispatchMain
  • 1,617
  • 2
  • 18
  • 30
0

you don't need to dismiss ViewController in the completion handler, it will be handled when you press cancel button

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
    SLComposeViewController * fbSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

    [controller setInitialText:initialText];
    [fbSheet addImage:myImage];
    [self presentViewController:controller animated:YES completion:Nil];        
}
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
  • I need to dismiss the View Controller that presents the SLComposeViewController if the user "posts" to Facebook. – syedfa Dec 27 '14 at 06:00