How to detect loss of internet reachability when wifi source loses connection?

Ahmed picture Ahmed · Feb 28, 2013 · Viewed 7.3k times · Source

I am trying hard to get the authentic internet connectivity status. I have used Apple's Reachability but it's only giving me the status of wifi connectivity i.e. not covering the case when the device is connected via wifi (to a router or hotspot), but the wifi router or hotspot itself is not connected to the internet. This scenario is reproducible by pulling the internet input cable from wifi router. The reachability's notifier returns ReachableViaWiFi for both reachabilityForInternetConnection and ReachabilityWithHostName. I am quite much in a fix with this issue. I tried it via the NSURLConnection too but that's draining battery too much and personally I don't like that solution to keep making URL requests, though in a background thread.

Here's the code that I am using (courtesy of an SO fellow, don't remember the exact link though)

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

    internetReachable = [[Reachability reachabilityForInternetConnection] retain];
    [internetReachable startNotifier];

    // check if a pathway to a random host exists
    hostReachable = [[Reachability reachabilityWithHostName: @"www.google.com"] retain];
    [hostReachable startNotifier];

then in the observer method:

 - (void) checkNetworkStatus:(NSNotification *)notice{

    // called after network status changes

    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)

    {
        case NotReachable:
        {
            NSLog(@"The internet is down.");
            self.isInternetReachable = NO;

            break;

        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");
            self.isInternetReachable = YES;

            break;

        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");
            self.isInternetReachable = YES;

            break;

        }
    }
    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)

    {
        case NotReachable:
        {
            NSLog(@"A gateway to the host server is down.");
            self.isHostReachable = NO;

            break;

        }
        case ReachableViaWiFi:
        {
            NSLog(@"A gateway to the host server is working via WIFI.");
            self.isHostReachable = YES;

            break;

        }
        case ReachableViaWWAN:
        {
            NSLog(@"A gateway to the host server is working via WWAN.");
            self.isHostReachable = YES;

            break;

        }
}
}

Answer

nsgulliver picture nsgulliver · Feb 28, 2013

According to Reachability it checks for not only wifi but also for WWAN/3G and no internet access, why do you think it does not check other than WIFI?

 typedef enum {

    NotReachable = 0,

    ReachableViaWiFi,

    ReachableViaWWAN

} NetworkStatus;

if you want to check the reachability to a particular host then you could set the host yourself like this

Reachability *hostReach = [Reachability reachabilityWithHostName: @"www.google.com"];

NetworkStatus netStatus = [hostReach currentReachabilityStatus];

 switch (netStatus)
    {
        case ReachableViaWWAN:
        {
            NSLog(@"WWAN/3G");
            break;
        }
        case ReachableViaWiFi:
        {
            NSLog(@"WIFI");
            break;
        }
        case NotReachable:
        { 
            NSLog(@"NO Internet");
            break;
        }
}