5

I just want to know how to detect internet connectivity in iOS when device is connected to wifi but no intenet connection in modem.

I have used apple's and AFNetworking's reachability but they only check for connectivity and returns connected flag even there is no working internet in modem.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
  • 1
    Presumably you're writing an app and want to know if the backend's accessible? Then just *try calling the backend* and handle the failure. – jonrsharpe Aug 25 '16 at 07:03
  • @jonrsharpe I just want to avoid hitting web api when there is no connection. – Mayank Jain Aug 25 '16 at 07:04
  • Well you won't be able to hit the web API when there's no connection, so that's not a problem. – jonrsharpe Aug 25 '16 at 07:07
  • @jonrsharpe it would be great if you can help me with the question instead of conceiving me to use other alternatives. Thanks. – Mayank Jain Aug 25 '16 at 07:09
  • 2
    I am helping with the question. You don't actually care about whether the device has connectivity, you care about *whether or not it can talk to the backend*. You might have Internet access but some firewall problem, or the server might be down, or ... - you still need to deal with that the same way. There's no reliable way to check the connection without touching something remote, and that something might as well be the something you actually want to touch. It would be great if you listened to the advice you came here asking for. – jonrsharpe Aug 25 '16 at 07:27
  • I completely agree with @jonrsharpe, maybe you can try a ping to google.com or stackoverflow.com (just examples), which should be *up&running at any time* – ddb Aug 25 '16 at 07:32
  • @ddb which still doesn't tell them if a call to the backend will work. You have to handle that case either way, might as well make it useful. – jonrsharpe Aug 25 '16 at 07:33
  • yes, @jonrsharpe, but it tells if device has *real* connectivity (modem can reach internet). Timeout connection error to web server anyway should be handled *somewhere in the code* – ddb Aug 25 '16 at 07:36
  • @ddb but now you're making two calls to answer the actual question, and it just means more code paths to test and maintain. – jonrsharpe Aug 25 '16 at 07:40
  • yes, @jonrsharpe, you're right *in general*, but it depends what's the objective. It can be useful to show a warning message "please, server unreachable, try again later" if web server is down but device is connected to internet. But of course connection check to web server must be handled – ddb Aug 25 '16 at 07:43
  • @jonrsharpe I agreed, but call webapi or backend to check internet connectivity might not be a good approach in respect with performance. It will work but not instant it will take some time to send request and get response from server. I am totally agreed with ddb we need to ping for host if it is responding or not. I found the solution and your suggestion worked. Thank you both. – Mayank Jain Aug 25 '16 at 08:00
  • @ddb I am totally agreed, we need to ping for host if it is responding or not. I found the solution and your suggestion worked. Thank you. – Mayank Jain Aug 25 '16 at 08:01
  • 1
    @MayankModi, then if you agree I'll write an answer on it that you could accept – ddb Aug 25 '16 at 08:03
  • @ddb I found my answer. If you wanna share your code you can. It will also help others. Thanks – Mayank Jain Aug 25 '16 at 08:08

2 Answers2

2

I found the solution by hitting host via Reachability:-

Might be helpful for someone.

-(BOOL)isCheckConnection {
    Reachability *rc = [Reachability reachabilityWithHostName:@"www.google.com"];
    NetworkStatus internetStatus = [rc currentReachabilityStatus];

    if(internetStatus==0)
    {
        //@"NoAccess";
        return NO;
    }
    else if(internetStatus==1)
    {
        //@"ReachableViaWiFi";
        return YES;
    } else if(internetStatus==2)
    {
        //@"ReachableViaWWAN";
        return YES;
    }
    else
    {
        //@"Reachable";
        return YES;
    }
}
Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
1

In order to be sure that your device is really connected to internet, at least you have to try a ping to a up&running at anytime server (like stackoverflow.com o google.com).

Of course, as suggested by @jonrsharpe in question's comments, if this check is necessary in order to understand if app can reach the web server, then a ping or something like that to your web server is also necessary.

But a ping to a surely working web server (like google) will give you the answer it your device is connected to internet (so your modem can reach internet), that way if your web server is not responding you can accordingly show a warning alert in your app to inform user that currently your server is unreachable.

Going to code, you can try to check connection status with Reachability library like below

Reachability *reachability = [Reachability reachabilityWithHostName:@"stackoverflow.com"];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];

and then check networkStatus variable: if 0 you don't have access to internet, otherwise YES, so

if(networkStatus==0) {
    // no access to internet
} else {
    // you have access to internet
}
ddb
  • 2,423
  • 7
  • 28
  • 38
  • 2
    I have connected via wifi but no internet is accessible it is still showing connected flag – Mayank Jain Aug 25 '16 at 08:46
  • maybe [this answer](http://stackoverflow.com/a/3597085/3178454) can be even more lightening than mine :) – ddb Aug 25 '16 at 08:48