Incorrect Response - Reachability For WWAN

2019-08-04 01:16发布

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.

`

- (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条回答
forever°为你锁心
2楼-- · 2019-08-04 01:48

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.

查看更多
登录 后发表回答