0

I'm using the Reachability class provided by Apple and I'm listening for the reachabilityChanged: notification:

- (void)reachabilityChanged:(NSNotification *)notification
{
   Reachability *reach = [notification object];

   if ([reach isKindOfClass: [Reachability class]]) {
       NetworkStatus status = [reach currentReachabilityStatus];

       if ((status == ReachableViaWiFi) || (status == ReachableViaWWAN)) {
          // Some operations here
       }
   }
}

When I set flight mode or disable WiFi and cellular data in settings, I get a "NotReachable" status in reachabilityChanged:, but when my device seems to just lose the connectivity temporarily for whatever reason (having communications enabled in settings), the "NotReachable" status seems to not being detected.

I've done some tests with a jammer, and when the device goes to "No Service" in the status bar and reachabilityChanged: is called, I've seen that the NetworkStatus is "ReachableViaWWAN" instead of "NotReachable" as I expected.

I need to detect such "No Service" status in the device, how could I?

Siyual
  • 16,415
  • 8
  • 44
  • 58
AppsDev
  • 12,319
  • 23
  • 93
  • 186

1 Answers1

0

Credit goes to this person. This worked for me:

- (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;
 }

And then in the ReachableViaWWAN case:

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;
    }

}
Community
  • 1
  • 1
mnearents
  • 646
  • 1
  • 6
  • 31