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

3

8

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;

        }
}
}
Incursion answered 28/2, 2013 at 22:6 Comment(2)
Hi @Ahmed. Did you ever find a solution to this? I'm having the exact same issue - my users have their devices connected to a Wifi hotspot, and when the hotspot loses connectivity the app doesn't know.Kamp
2016 and still it's unsolved, got the same issue.Mailable
V
7

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;
        }
}
Vasques answered 28/2, 2013 at 22:9 Comment(8)
I am doing exactly what you stated for host reachability but I end up getting ReachableViaWiFi each time?Incursion
because you might be connected to WIFI that moment of time, try to disable WIFI of your device and then try and see what you get for status?Vasques
@Incursion did it solve your issue? is it first time you are using Reachability?Vasques
you haven't read my question completely. I know that Reachability will cater the unavailability of wifi very well. My concern and point of interest is to get notified or at least know that internet is not working though wifi is connected. I've explained on of the scenario in my question.Incursion
well I read your question, the moment internet is disconnected, the not reachable status would be shown, you should make sure that you are registering for the notification of internet change, because without that it would not helpVasques
yes I am adding observer for that. Check my code in updates. ThanksIncursion
It's working in it's capacity, I guess, but I need to know if the reachability can give me any clue to know the occurrence of the scenario that I stated? Thanks for your timeIncursion
for the scenario you are talking about, i am afraid, reachability would not handle in that way, it only gives use the status according to the changes happen on the connection, even in a case where WIFI connection is working but very slow, it might only gives us the status only. work around is to just send a request with timeout to a specific server & get notifiedVasques
C
4

See Jordan's Answer. There is a stupid bug in Apple's official Reachability.m. The type for returnValue should be "NetworkStatus" instead of "BOOL":

- (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags
{
    ...
    NetworkStatus returnValue = NotReachable;
    ...
}

Otherwise, you end up with the following terrible consequences:

  1. Even if cellular connection is available, it will be reported as unavailable.
  2. If Wifi is unavailable, Wifi will be reported as available if cellular connection is available.

Can't believe Apple never bothered to fix this.

(P.S. New to stack overflow and have no reps to upvote Jordan)

Commotion answered 28/1, 2014 at 0:48 Comment(0)
C
0

I was getting too the Wifi flag even with it disconnected. There is a bug in the Reachability.m method:

- (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags

it uses a BOOL as return value, but it assigns to it the values of a struct with 3 values:

typedef enum : NSInteger {
    NotReachable = 0,
    ReachableViaWiFi,
    ReachableViaWWAN
} NetworkStatus;

So if whether Wifi or Cellular are available, it will ReachableViaWiFi (as a BOOL can be 0 or 1, but not two)

To fix it, just change in the method above this:

BOOL returnValue = NotReachable;

For this:

int returnValue = NotReachable;

And you're good to go. Hope it helps.

Centare answered 7/11, 2013 at 15:24 Comment(1)
I changed the BOOL to NetworkStatus (which is the correct type) but it still doesn't work for me. I still get ReachableViaWiFi even when WiFi is off.Eberle

© 2022 - 2024 — McMap. All rights reserved.