Facebook与SLComposeViewController共享:当没有可用的互联网时,防止完成处理程序

时间:2013-05-07 04:42:17

标签: facebook ios6

我在我的应用程序(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. 互联网不可用,用户输入文字并按下帖子。
  4. 前两个应该工作。在第三种情况下,正如预期的那样,我得到警报

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

    但是在我按下提示给我的警报视图中的“再试一次”或“取消”按钮后,我收到“已发布”警报(完成处理程序类型SLComposeViewControllerResultDone被执行)。

    如何防止这种情况?

2 个答案:

答案 0 :(得分:1)

编辑: 嗯,修复第三种情况很简单。我添加了Apple提供的可访问性类(可供下载here。)。只需要的代码如下:

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

答案 1 :(得分:1)

请检查如果要将URl添加到它(SLComposeViewController),它必须是http://www.stackoverflow.com格式化,否则它将继续显示 “无法发布到Facebook” - 由于与Facebook的连接失败,因此无法发送帖子。

HTH