Facebook与SLComposeViewController共享:在没有互联网可用时阻止完成处理程序 [英] Facebook sharing with SLComposeViewController: Prevent completion handler when no internet is available

查看:116
本文介绍了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];
}

我正在测试以下情况:


  1. 互联网可用,用户输入文字并按下帖子

  2. 互联网可用,用户输入文字并按下取消

  3. 互联网不可用,用户输入文字并按下帖子。

前两个工作正常。在第三种情况下,正如预期的那样,我得到提醒

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;

这篇关于Facebook与SLComposeViewController共享:在没有互联网可用时阻止完成处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆