我在我的应用程序(iOS6的)实现的Facebook共享和代码如下。
//完成处理程序
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;
}
}
//发布至Facebook
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *fbVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
fbVC.completionHandler = completionHandler;
[self presentViewController:fbVC animated:YES completion:nil];
}
我测试了以下几种情况:
- 互联网提供与用户输入的文本和压后
- 互联网提供与用户输入的文本,并按下了取消
- 网络不可用,用户输入的文本和压后。
前两部作品,因为他们应该。 在第三种情况下,如预期,我得到警报
"Cannot Post to Facebook" - The post cannot be sent because connection to Facebook failed.
但经过我请按再试,或在提交给我警报视图取消按钮,我得到“发布”警报(完成处理器类型SLComposeViewControllerResultDone被执行)。
如何避免这种情况?