iOS可达性测试

标签 ios reachability internet-connection

对于我们的应用程序,每当应用程序用户尝试发布消息时,我们都会使用以下代码检查互联网连接。当我们测试该功能时,它在打开飞行模式时工作正常。然后当我们关闭飞行模式时,对connected的调用仍然返回NO。这可能是什么原因?我们是否需要在订单中进行额外的“设置”才能正确执行?比如监听网络状态变化通知?

+ (BOOL)connected 
{
    Reachability *hostReach = [Reachability reachabilityForInternetConnection]; 
    NetworkStatus netStatus = [hostReach currentReachabilityStatus];    
    return !(netStatus == NotReachable);
}

最佳答案

Apple 工程师建议完全依赖 Rechability。

来自SO post ( block 引用的来源)

On a WWDC talk this year the Apple engineer on stage recommended users to never base 
the application internet access on the Reachability example app status. Often     
reachability doesn't provide a complete information (it is based on a complex mechanism)    
and the suggestion provided by the engineer was this:

1. try to do your internet connection, whatever it is the Reachability status; 
   then set your UI hint based on success/fail result
2. if it fails due to networking issue, then register to Reachability and retry again 
   when Reachability gives the green light; this is needed when you want to recover 
   automatically from the fail condition
3. in any case give the user the possibility to "force a retry", whatever is the 
   Reachability status. If it succeeds, reset your UI hint immediately.

我做了什么?

例如每次我需要建立连接时

NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString 
               stringWithFormat:@"http://myadress.com"]]];
[self performSelectorOnMainThread:@selector(responseHandler:)
                       withObject:data waitUntilDone:TRUE];


- (void)responseHandler:(NSData *)responseData {
    if(!responseData) {
       ReachabilityController *reachability = [[ReachabilityController alloc] init];
       [reachability checkReachability];
       return;
    }
   // you handle your data
}

那里发生的事情是,只有在连接失败时才会测试可达性。我制作了一个仅处理可达性的通用 ReachabilityController。我这样做了,这样我就可以在每次发出请求时从所有其他 Controller 进行调用。

我的 ReachabilityController.m 看起来像

-(void) checkReachability {
     Reachability* internetAvailable = [Reachability reachabilityForInternetConnection];
     NetworkStatus netStatus = [internetAvailable currentReachabilityStatus];
     NSString *messageText;
     if (netStatus == NotReachable)
     {
         messageText = [NSString stringWithFormat:@"Internet access not available"];
     }
     else
     {
          Reachability *netReach = [Reachability reachabilityWithHostName:host];
          NetworkStatus hostStatus = [netReach currentReachabilityStatus];
          if (hostStatus == NotReachable)
          {
              messageText = [NSString stringWithFormat:@"Host Unreachable"];

          }
          else
          {
              messageText = [NSString stringWithFormat:@"Problem with remote service"];
          }

     }
     NSLog(@"%@", messageText);   
}

它不会使您的应用程序崩溃,因为您正在自己处理“nil”数据参数,最终您正在确定原因。

希望这对您有所帮助!

更新:

如果您显示有关互联网连接的错误消息,Apple 可能会拒绝您的应用。他们非常重视自己在 iPhone 上的声誉。如果互联网连接可用,但如果您的应用报告互联网连接不可用,将被视为非常严重

关于iOS可达性测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11055897/

相关文章:

ios - 在 Swift 中使用可达性

ios - NetworkReachabilityManager 或 Reachability 无法识别在没有互联网的情况下连接 wifi

iphone - Objective-C 判断 iOS 设备的数据网络类型

ios - `didFailLoadWithError:` 问题

c# - 不断检查互联网连接

ios - Nimbus 与使用 cocoapods 的 iOS 4.3 不兼容

ios - 对 NSArray 的 NSArray 中的 objectAtIndex 值求和或将值与 NSDictionary 中的键的现有值相加

ios - Multipeer Connectivity 数据仅以一种方式发送,即使两个设备都已连接

ios - 使用表格数据进行 CoreML 设备端模型训练

ios - 当互联网 (2G) 连接非常慢时通知用户 - iOS