1

I saw any post about Reachability but people doesn't really give the exact answer to the problem. In my application I use the Reachability code from apple and in my appDelegate I use this:


-(BOOL)checkInternet {

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

NetworkStatus internetStatus = [reachability currentReachabilityStatus];
BOOL internet;

if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
    internet = NO;
}else {
    internet = YES;
}
return internet;    
}

So the problem is even if I have an internet connection, this code telling me that I don't have one. Does anyone know what to do to make this working?

Thanks,

ludo
  • 1,479
  • 5
  • 25
  • 50
  • Just try this code it will work perfectly. http://stackoverflow.com/questions/8356656/reachability-crashes-app/14452743#14452743 – SURESH SANKE Jan 22 '13 at 06:27

2 Answers2

7

You should probably be using +[Reachability reachabilityForInternetConnection] rather than reachability for a particular name (unless of course that's what you actually need).

There could be all manner of reasons that a particular server might not be reachable while you still have a working internet connection, after all.

This is what I do:

BOOL hasInet;
Reachability *connectionMonitor = [Reachability reachabilityForInternetConnection];
[[NSNotificationCenter defaultCenter]
    addObserver: self
    selector: @selector(inetAvailabilityChanged:)
    name:  kReachabilityChangedNotification
    object: connectionMonitor];

hasInet = [connectionMonitor currentReachabilityStatus] != NotReachable;

and then

-(void)inetAvailabilityChanged:(NSNotification *)notice {
    Reachability *r = (Reachability *)[notice object];
    hasInet = [r currentReachabilityStatus] != NotReachable;
}

which works nicely for me.

Frank Shearar
  • 17,012
  • 8
  • 67
  • 94
  • thanks but I also find another way to solve my problem. By reading the Reachability_readme.txt, they said that the connection can take time by trying to identify the hostName (and so fail sometimes), so I simply use the google DNS like (74.125.71.104) and now its working fine and its fast too. – ludo Apr 19 '10 at 08:07
  • Hi ludo and Frank, I used your code and the Reachability sample code, but it still cannot detect the Internet's availability correctly. How do you use the ip address 74.125.71.104? I guess should not be reachabilityWithAddress: (const struct sockaddr_in*) hostAddress; or reachabilityWithHostName: (NSString*) hostName? Thanks. – lionfly Oct 11 '10 at 16:23
  • Hi ludo can you share your code to me for checking internet connectivity. – Suraj Mirajkar Oct 31 '11 at 12:00
  • Hi I have a problem. I am using reachabilityForInternetConnection method for detecting internet availability but instead of that I am getting status of connection and not status of internet. I mean if I turn of my Wifi connection, the method gives me correct indication that I dont have connection but if wifi is on and internet connection is not working, it does not seem to work. Any idea? – Aqueel Mar 07 '12 at 07:48
  • 2
    I think you forgot to call [_connectionMonitor startNotifier]; – Mert Sep 07 '12 at 10:08
0

Use this code to check whether the device is connected to internet or not

use this code in viewDidLoad :

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

    hostReachable = [Reachability reachabilityWithHostName: @"www.apple.com"] ;
    [hostReachable startNotifier];

and add this function to your code:

-(void) checkNetworkStatus:(NSNotification *)notice
{
    recheabilityBool=FALSE;
    nonrecheabilityBool=FALSE;
    // called after network status changes
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
        {
            nonrecheabilityBool=TRUE;
            internetCon=0;
            //NSLog(@"The internet is down.");


            break;
        }
        case ReachableViaWiFi:
        {
            NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
            internetCon=404;
            [prefs setInteger:internetCon forKey:@"conKey"];

            //NSLog(@"The internet is working via WIFI.");
            break;

        }
        case ReachableViaWWAN:
        {
            //NSLog(@"The internet is working via WWAN.");

            break;
        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)
    {
        case NotReachable:
        {
            internetCon=0;
            if( nonrecheabilityBool==FALSE)
            {
                //NSLog(@"A gateway to the host server is down.");
            }
            break;

        }
        case ReachableViaWiFi:
        {
            if(recheabilityBool==FALSE)
            {

                recheabilityBool=TRUE;

                NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
                internetCon=404;
                [prefs setInteger:internetCon forKey:@"conKey"];


                //NSLog(@"The internet is working via WIFI.");
                break;
            }


            //NSLog(@"A gateway to the host server is working via WIFI.");

            break;
        }
        case ReachableViaWWAN:
        {
            //NSLog(@"A gateway to the host server is working via WWAN.");
            break;
        }
    }
}

- (BOOL)connected
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return !(networkStatus == NotReachable);
}
Nithinbemitk
  • 2,710
  • 4
  • 24
  • 27