iOS可达性测试 [英] iOS Reachability test

查看:97
本文介绍了iOS可达性测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我们的应用,我们使用以下代码检查应用用户尝试发布消息时的互联网连接。当我们测试该功能时,它在打开飞行模式时工作正常。然后当我们关闭飞行模式时,对连接的呼叫仍然返回NO。可能是什么原因?我们是否需要在订单中额外设置以使其正确?如收听网络状态变更通知?

For our app, we use the following code to check for internet connection whenever the app user is trying to post a message. When we test the feature, it works fine when turning on the airplane mode. Then when we turn off the airplane mode, the call to connected still returns NO. What could be the reason for that? Do we need extra "setup" in the order in order to get it right? such as listen to network state change notifications?

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


推荐答案

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

Apple Engineers have suggted that completely relying on Rechability.

来自 SO帖子(blockquote来源)

From a SO post (source of blockquote)

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.

我做了什么?

每一个时间我需要建立一个连接例如

Every time I need to make a connection for example

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,只处理可达性。我这样做了,这样每次我发出请求时都可以从所有其他控制器拨打电话。

What is happening there is, the reachability will only be tested if the connection was failed. I have made a generic ReachabilityController that only deals with the reachability. I did it so, such that i could make calls from all the other controllers every time i make a request.

我的ReachabilityController.m看起来像

My ReachabilityController.m looks like

-(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数据参数你自己,最后你确定原因。

It won't crash your app, As you are handling the "nil" data parameter yourself and finally you are determining the cause.

希望这有帮助!

更新:

如果您显示有关互联网连接的虚假消息,Apple可能会拒绝您的应用。他们对iPhone的声誉非常认真。如果互联网连接可用,但如果您的应用报告互联网连接不可用,将被视为非常严重

Apple may reject your app if you show a false message about the Internet Connectivity. They are very serious about their reputation with the iphone. If the internet connectivity is available but if your app reports that internet connectivity is unavailable, It will be considered very serious

这篇关于iOS可达性测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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