I have implemented Facebook sharing in my app (iOS6) and the code is as follows.
//Completion Handler
SLComposeViewControllerCompletionHandler __block completionHandler = ^(SLComposeViewControllerResult result) {
UIAlertView *alert = nil;
switch(result) {
case SLComposeViewControllerResultCancelled: {
alert = [UIAlertView alloc]initWithTitle:@"Cancelled" message:@"Your message wasn't shared" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
break;
case SLComposeViewControllerResultDone: {
alert = [UIAlertView alloc]initWithTitle:@"Posted" message:@"Your message was posted successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
break;
}
}
// Posting to Facebook
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *fbVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
fbVC.completionHandler = completionHandler;
[self presentViewController:fbVC animated:YES completion:nil];
}
I am testing the following situations:
- Internet available and user entered text and pressed post
- Internet available and user entered text and pressed cancel
- Internet not available and user entered text and pressed post.
First two works as they should. In the third situation, as expected, I get alert
"Cannot Post to Facebook" - The post cannot be sent because connection to Facebook failed.
But after I press either Try Again or Cancel button in the alert view that was presented to me, I get "Posted" alert (the completion handler type SLComposeViewControllerResultDone gets executed).
How to prevent this?