Incorrect Response - Reachability For WWAN

旧巷老猫 提交于 2019-12-11 14:46:31

问题


While checking for reachability(network availability) of an iPAD 3 WiFi+Cellular, I ran into across a weird issue that happened in the below mentioned scenario.

  • To check network availability I have used Apple sample code for reachability.
  • The following code was implemented to check availability of WiFi or WWAN.

`

- (BOOL)networkCheck
 {
    Reachability *wifiReach = [Reachability reachabilityForInternetConnection];
    NetworkStatus netStatus = [wifiReach currentReachabilityStatus];

    switch (netStatus)
    {
        case NotReachable:
        {
            NSLog(@"%@",@"NETWORKCHECK: Not Connected");    
            return false;
            break;
        }
        case ReachableViaWiFi:
        {
            NSLog(@"%@",@"NETWORKCHECK: Connected Via WiFi");       
            return true;
            break;
        } 
        case ReachableViaWWAN:
        {
            NSLog(@"%@",@"NETWORKCHECK: Connected Via WWAN");
            return true;
            break;
        }
    }
    return false;
 }

`

  • In a scenario when there was NO SIM in the iPAD & also there was No-WiFi connection, the above method executes ReachableViaWWAN case, which seems totally incorrect as there is NO SIM or any other WWAN network available.

To Overcome this issue a workaround (or should I say a hack) was suggested & implemented as follows: Send a request to a reliable host & check for its response.

case ReachableViaWWAN:
    {
        NSLog(@"%@",@"NETWORKCHECK: Connected Via WWAN");
        NSData *responseData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"www.google.com"]] returningResponse:nil error:nil];
        if (responseData != nil)
        {
            return true;
            break;
        }
        else
        {
            return false;
            break;
        }

    }

I have a couple of queries:

  1. This may sound offbeat, but is it something wrong with the hardware or iOS that it's ReachableViaWWAN even when NO SIM is present in the device?
  2. Is there a better solution (than the workaround mentioned above) for the problem?

回答1:


I don't know if it is a bug in your code but you can try using this to check the connection. from this question.

and for check the connectivity you can create a method like this:

 -(BOOL)connected
  {
     Reachability *reachability = [Reachability reachabilityForInternetConnection];
     NetworkStatus networkStatus = [reachability currentReachabilityStatus];
     return !(networkStatus == NotReachable);
 }

and wherever you want to check the connection just

if (![self connected]) {
//THERE IS NO WWAN OR WIFI connection
}else{
// connected. 
}

It works fine for me.



来源:https://stackoverflow.com/questions/17495221/incorrect-response-reachability-for-wwan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!