Facebook与SLComposeViewController共享:在没有互联网可用时阻止完成处理程序
我在我的应用程序(iOS6)中实现了Facebook共享,代码如下。
I have implemented Facebook sharing in my app (iOS6) and the code is as follows.
//完成处理程序
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
// Posting to Facebook
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *fbVC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
fbVC.completionHandler = completionHandler;
[self presentViewController:fbVC animated:YES completion:nil];
}
我正在测试以下情况:
- 互联网可用,用户输入文字并按下帖子
- 互联网可用,用户输入文字并按下取消
- 互联网不可用,用户输入文字并按下帖子。
前两个工作正常。在第三种情况下,正如预期的那样,我得到提醒
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.
但是在我提交给我的警报视图中按下再试一次或取消按钮后,我获取已发布警报(完成处理程序类型SLComposeViewControllerResultDone被执行)。
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).
如何防止这种情况?
编辑:$ b $嗯,解决第三种情况很简单。我添加了Apple提供的可访问性类(可下载此处 。)只需要的代码如下:
Well, it was simple to fix the third situation. I added the reachability class provided by Apple (available for download here.) Only code that was required is as follows:
#import "Reachability.h"
- (BOOL)internetConnected {
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable || reachability.connectionRequired); //required for iOS 7 and above
}
...
...
case SLComposeViewControllerResultDone: {
if(self.internetConnected) {
alert = [UIAlertView alloc]initWithTitle:@"Posted" message:@"Your message was posted successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
} else {
alert = [UIAlertView alloc]initWithTitle:@"Failed" message:@"Your message was not posted, no internet was available" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
}
break;