Monday, July 16, 2012

Importance of SLComposeViewControllerCompletionHandler

You need to add the handler(SLComposeViewControllerCompletionHandler) otherwise you may get following error when you try to cancel the Facebook Post or post the Post:

viewServiceDidTerminateWithError:: Error Domain=_UIViewServiceErrorDomain Code =1 "The operation couldn't be completed. (_UIViewServiceErrorDomain error 1.)"


You need to create handler like following

SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result)
{
            
            [fbController dismissViewControllerAnimated:YES completion:nil];
            
            switch(result){
            case SLComposeViewControllerResultCancelled:
            default:
            {
                NSLog(@"Cancelled.....");
                
            }
                break;
            case SLComposeViewControllerResultDone:
            {
                NSLog(@"Posted....");
            }
                break;
        }};


and add this handler to SLComposeViewController


SLComposeViewController *fbController=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fbController setCompletionHandler:completionHandler];

and you can see the last post to see how to use it completely with the sample code. LINK.



2 comments:

  1. I'm glad I found this post... I was trying to figure out how to use the completion handler and get the twitter panel to dismiss properly. One small change though... the __block keyword, I believe, is unnecessary here. That keyword allows you to both read and write to a particular variable from within the block. As a block has access to itself it does not need the keyword.

    That all being said, your post helped me out a great deal!
    Thanks!

    ReplyDelete
  2. Hi Dan,

    __block was needed because I was running my code on ARC. Hope this clarifies it.

    ReplyDelete