4

My code for checking for an Internet connection, I discovered yesterday on an airplane, is excruciatingly slow. I am using the technique in many older SO answers of checking an NSUURL, however, it literally took fifteen seconds or more to return FALSE when on an airplane with no connectivity making the app essentially unusable.

Is there a dependable asynchronous method that would do this? People recommend Tony Million's reachability class in older answers but it seems very complicated and Apple is apparently rejecting some apps using it. Would appreciate it if someone could suggest the fastest, reliable way to check Internet connection in 2017. Thank you.

- (BOOL)connected
    {
        NSURL *scriptUrl = [NSURL URLWithString:@"https://www.google.com"];
        NSData *data = [NSData dataWithContentsOfURL:scriptUrl];
        if (data) {
            NSLog(@"Device is connected to the internet");
            return TRUE;
        }
        else {
            NSLog(@"Device is not connected to the internet");
            return FALSE;
        }
    }
Community
  • 1
  • 1
zztop
  • 701
  • 1
  • 7
  • 20
  • 2
    Respect for chartering a private jet for your testing environment. Have an upvote for that. – Bathsheba Feb 28 '17 at 14:42
  • 1
    This is Apple's own sample code. https://developer.apple.com/library/content/samplecode/Reachability/Listings/Reachability_Reachability_h.html – Dare Feb 28 '17 at 14:44
  • how to improve Xcode IDE;) – zztop Feb 28 '17 at 14:46
  • 1
    Apple's code seems incredibly complicated for checking connectivity. They suggest you build a separate project in Xcode to see how it works. Can this really be the best way? – zztop Feb 28 '17 at 19:23
  • I link to the question that is said to be a duplicate in the second line of my question. The reason I reasked the question in 2017 is that the accepted answer in the linked to question is from 2009. Marian provides a convincing updated answer below. – zztop Mar 01 '17 at 22:47

1 Answers1

3

Reachability is the correct way... even in 2017.

All the various Reachability classes are based on Apple's sample code.

It might seem complicated because it uses C APIs. But usage is simple:

- (BOOL)connected
{
    Reachability *reach = [Reachability reachabilityForInternetConnection];

    if ([reach isReachable]) {
        NSLog(@"Device is connected to the internet");
        return TRUE;
    }
    else {
        NSLog(@"Device is not connected to the internet");
        return FALSE;
    }
}

If you will be using Apple's Reachability class instead of Tony Million's, replace [reach isReachable] with [reach currentReachabilityStatus] != NotReachable.

Don't worry that Apple might reject an app using it. If that happens to you, just rename the class from Reachability to MyReachability.

Marián Černý
  • 15,096
  • 4
  • 70
  • 83
  • 3
    Talking about Wi-Fi connections, this method can only tell if you are connected to a router. It doesn't tell, if that router is actually connected to the Internet. – tonso Oct 12 '18 at 22:29