can not detect internet connection with Reachability reachabilityForInternetConnection
Asked Answered
A

2

7

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

Ackerman answered 7/3, 2012 at 7:56 Comment(1)
I'm also experiencing this kind of issue, I wonder why people doesn't usually solve this and just use the Reachability. I mean what happens with there app if it is connected to wifi and internet is actually not working. And how do they handle this caseStructure
B
3

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 :)

Bypath answered 7/3, 2012 at 8:43 Comment(4)
what if my app is being used by millions of people around the world? Should I send a large bunch (if thousands of users at a time using my app) of these network status check request from my app to google or Apple? Is it ok to do that?Ackerman
It depends how often you have to check the reachability of your app - try to reduce the number of times to as few as possible. Otherwise I guess yes, it's ok - google.com is being queried by billions of people in every minute....Bypath
@Ackerman - Presumably you have an actual request to make? You should make it. That is the only way to know if it will work. What is the point of knowing if google is reachable if the actual request you want to make is foobar.org/users?1213321321312&images=med ?Vouch
@Vouch Generally you are right, but querying a highly reliable server like google.com can tell you the information whether your device is connected to the Internet. If you query foobar.org and it fails it is possible that your device can't connect to the Internet but also that only foobar.org is down so with google.com you get a clearer picture. As a third step you can try also foobar.org to see whether the server is up.Bypath
N
2

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;
        }
    }
}
Necrophilism answered 7/3, 2012 at 9:0 Comment(1)
what if my app is being used by millions of people around the world? Should I send a large bunch (if thousands of users at a time using my app) of these network status check request from my app to google or Apple? Is it ok to do that?Ackerman

© 2022 - 2024 — McMap. All rights reserved.