3

我已经在我的应用程序(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 被执行)。

如何防止这种情况?

4

2 回答 2

1

编辑:嗯,解决第三种情况很简单。我添加了 Apple 提供的可达性类(可在此处下载。)仅需要的代码如下:

#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;
于 2013-05-07T05:35:08.153 回答
1

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

高温高压

于 2014-04-04T07:32:12.443 回答