Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reachability not working when wi-fi connected but no internet

I am using Apple's reachability code, and am setting up both initial notifications on when network reachability changes and prior to initiating a server connection. The code works when I am on wi-fi and I turn the wi-fi access point off. However, when I start the app with wi-fi and the underlying broadband connection working, and then once the app is running, and then disconnect the wi-fi router from the broadband router (i.e. Wi-Fi is on but there is no internet connectivity), and I do a reachability check, the network status I get is ReachableViaWiFi. I have tried both reachabilityForInternetConnection and reachabilityWithHostName.

Any ideas on if Apple's reachability code can be used to detect a situation where the wifi is connected but there is no underlying network connectivity?

Thanks!

like image 991
R.S Avatar asked Nov 16 '12 06:11

R.S


People also ask

Why is my WiFi connected but no Internet access?

If all your devices get no internet connection, yet your WiFi indicator is still on, the most obvious answer is that your internet provider has an outage. Before you start rebooting and shuffling wires around, it's always a good idea to check this first.

How do I fix WiFi when its connected but not working?

Restart your device. It might sound simple, but sometimes that's all it takes to fix a bad connection. If restarting doesn't work, switch between Wi-Fi and mobile data: Open your Settings app and tap Network & internet or Connections. Depending on your device, these options may be different.


2 Answers

ok, I found out the answer to this - Apple's reachability does not test actual connectivity to the host. See the answer by @Zhami in the SO link below:

How to write a simple Ping method in Cocoa/Objective-C

Essentially, when you first launch the app and do a reachability check, iOS seems to do a DNS lookup, and if there is no internet, the check fails. So the first time you check reachability , it actually returns a meaningful value. However, if you are conected at app launch, and lose internet connectivity after some time (while still connected to WiFi/3G/4G but no underlying internet connectivity), further reachability checks return reachable even though the internet or your specified host is not reachable anymore.

So if you want to really check for connectivity in real time, consider using the following:

-(BOOL) isConnected
{

    NSString* url = [NSURL URLWithString:@"http://www.google.com/m"];
    ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
    [request    setTimeOutSeconds:10];
//customize as per your needs - note this check is synchronous so you dont want to block the main thread for too long
    [request setNumberOfTimesToRetryOnTimeout:0];
    [request startSynchronous];

    NSError *error = [request error];
    if (error)
    {
        DLog(@"connectivity error");
        return NO;
    }
    else
    {
        DLog(@"connectivity OK");
        return YES;
    }

}
like image 134
R.S Avatar answered Sep 23 '22 17:09

R.S


This is a very old post, but could stay here for reference. In the reachability example class you can find the code below:

- (BOOL)startNotifier  
{  
    BOOL returnValue = NO;
    SCNetworkReachabilityContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};

   if (SCNetworkReachabilitySetCallback(_reachabilityRef, ReachabilityCallback, &context))
   {
        if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode))
        {
            returnValue = YES;
        }
   }

   return returnValue;
}

This will keep your _reachabilityRef updated to network changes.

like image 26
RicardoDuarte Avatar answered Sep 20 '22 17:09

RicardoDuarte