Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why SLComposeViewController doesn't have delegate?

Say you want to do something once user finish. What do you do?

It doesn't have a delegate. What to do once a present view controller is dismissed?

like image 325
user4951 Avatar asked Oct 21 '22 02:10

user4951


1 Answers

In the Apple documentation you'll find that SLComposeViewController has a completion handler property instead of a delegate. You just need to set that property using the setCompletionHandler method. Then you use the constant SLComposeViewControllerResult to recover whether the post was posted or canceled and take action accordingly.

-(void) shareToFacebook {
//1. Set link and image
NSString *appLink = @"https://itunes.apple.com/app/id989793966";
UIImage *twitterImage = [UIImage imageNamed:@"TF_400x400.png"];

//2. Check if we can share
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {

    //3. Compose the share view controller
    SLComposeViewController *FBViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

    [FBViewController addURL:[NSURL URLWithString:appLink]];

    [FBViewController addImage:twitterImage];

    //4 Set completion handler and define actions to take
    [FBViewController setCompletionHandler:^(SLComposeViewControllerResult result)
     {
         if (result == SLComposeViewControllerResultCancelled) {

             [self addEmptyScreenButtonTargets];

         } else if (result == SLComposeViewControllerResultDone) {

             //Unlock words; show thank you screen
             [NewCardManager unlockWordsForPackage:4];

             [self openFBThankYouScreen];  
         }
     }];

    //5. Call to modally present the share controller
    [self presentViewController:FBViewController animated:YES completion:nil];
}

}

like image 80
Mike Critchley Avatar answered Oct 24 '22 09:10

Mike Critchley