Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can not detect internet connection with Reachability reachabilityForInternetConnection

I have a problem. I am using reachabilityForInternetConnection method of Reachability 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?

Best Regards

like image 766
Aqueel Avatar asked Mar 07 '12 07:03

Aqueel


2 Answers

Reachability can be used only to detect whether the iPhone has a connection to a gateway to the internet. What is behind the gateway, it will not tell you. What if the LAN is reachable but you have no exit to the Internet? How could iPhone guess that what it sees (the LAN) is not the whole Internet?

You should make some real request to a real site. If it fails, there are some problem connecting to the Internet, and with the results of Reachability you can even understand where is the problem. The simplest way is to make a request with NSUrlRequest for example to http://www.google.com. (If google dies, you may suppose that there are bigger problems out there then your app's connectivity :)

like image 142
MrTJ Avatar answered Sep 29 '22 06:09

MrTJ


I use this in my app:

// Check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

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

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

and:

- (void) checkNetworkStatus:(NSNotification *)notice
{
    // Called after network status changes
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
            // Case: No internet
        case NotReachable:
        {
            internetActive = NO;

            // Switch to the NoConnection page
            NoConnectionViewController *notConnected = [[NoConnectionViewController alloc] initWithNibName:@"NoConnectionViewController" bundle:[NSBundle mainBundle]];

            notConnected.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
            [self presentModalViewController:notConnected animated:NO];

            break;
        }
        case ReachableViaWiFi:
        {
            internetActive = YES;
            break;
        }
        case ReachableViaWWAN:
        {
            internetActive = YES;
            break;
        }
    }

    // Check if the host site is online
    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)
    {
        case NotReachable:
        {
            hostActive = NO;
            break;
        }
        case ReachableViaWiFi:
        {
            hostActive = YES;
            break;
        }
        case ReachableViaWWAN:
        {
            hostActive = YES;
            break;
        }
    }
}
like image 33
user969043 Avatar answered Sep 29 '22 07:09

user969043