Facebook分享与SLComposeViewController:在没有互联网连接时防止完成处理程序

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)。

如何防止这种情况发生?

2个回答

1

编辑: 好的,修复第三种情况很简单。我添加了苹果提供的可达性类(可以在这里下载)。所需的唯一代码如下:

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

嗨@thandasoru,这不是好的或有利的,即使网络连接可用,如果用户在某段时间内再次发布相同的内容,它仍会给出此类错误(不知道多长时间)。如何处理这种情况? - The iOSDev
在我的应用程序中,我使用它来上传图像。因此,在我的代码中,我检查图像是否已经上传,如果是,则禁用分享按钮。如果您需要分享一些文本,可以将文本存储在UserDefaults中,并检查用户是否尝试再次上传相同的文本。然后,您可以显示“该文本已经被分享”警报。 - thandasoru

1

请检查是否将URL添加到了SLComposeViewController中,它必须采用http://www.stackoverflow.com的格式,否则会一直显示“无法发布到Facebook” - 发布失败,因为连接到Facebook失败。

希望对你有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接