与Facebook的共享SLComposeViewController:防止完成处理时没有网络可用(

2019-09-02 03:18发布

我在我的应用程序(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];
}

我测试了以下几种情况:

  1. 互联网提供与用户输入的文本和压后
  2. 互联网提供与用户输入的文本,并按下了取消
  3. 网络不可用,用户输入的文本和压后。

前两部作品,因为他们应该。 在第三种情况下,如预期,我得到警报

"Cannot Post to Facebook" - The post cannot be sent because connection to Facebook failed.

但经过我请按再试,或在提交给我警报视图取消按钮,我得到“发布”警报(完成处理器类型SLComposeViewControllerResultDone被执行)。

如何避免这种情况?

Answer 1:

请检查您是否添加URL以它(SLComposeViewController),它必须是http://www.stackoverflow.com格式化,否则它会继续显示“无法投递到Facebook” -该帖子无法发送,因为连接到Facebook的失败。

HTH



Answer 2:

编辑:嗯,这是简单的修复第三种情况。 我加入苹果公司提供的可达性类(提供下载这里 。)只有被要求的代码如下:

#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;


文章来源: Facebook sharing with SLComposeViewController: Prevent completion handler when no internet is available
标签: facebook ios6