iosmacoscocoacocoa-touchreachability

How can I check for an active Internet connection on iOS or macOS?


I would like to check to see if I have an Internet connection on iOS using the Cocoa Touch libraries or on macOS using the Cocoa libraries.

I came up with a way to do this using an NSURL. The way I did it seems a bit unreliable (because even Google could one day be down and relying on a third party seems bad), and while I could check to see for a response from some other websites if Google didn't respond, it does seem wasteful and an unnecessary overhead on my application.

- (BOOL)connectedToInternet {
    NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];
    return ( URLString != NULL ) ? YES : NO;
}

Is what I have done bad, (not to mention stringWithContentsOfURL is deprecated in iOS 3.0 and macOS 10.4) and if so, what is a better way to accomplish this?


Solution

  • Important: This check should always be performed asynchronously. The majority of answers below are synchronous so be careful otherwise you'll freeze up your app.


    Swift

    1. Install via CocoaPods or Carthage: https://github.com/ashleymills/Reachability.swift

    2. Test reachability via closures

      let reachability = Reachability()!
      
      reachability.whenReachable = { reachability in
          if reachability.connection == .wifi {
              print("Reachable via WiFi")
          } else {
              print("Reachable via Cellular")
          }
      }
      
      reachability.whenUnreachable = { _ in
          print("Not reachable")
      }
      
      do {
          try reachability.startNotifier()
      } catch {
          print("Unable to start notifier")
      }
      

    Objective-C

    1. Add SystemConfiguration framework to the project but don't worry about including it anywhere

    2. Add Tony Million's version of Reachability.h and Reachability.m to the project (found here: https://github.com/tonymillion/Reachability)

    3. Update the interface section

      #import "Reachability.h"
      
      // Add this to the interface in the .m file of your view controller
      @interface MyViewController ()
      {
          Reachability *internetReachableFoo;
      }
      @end
      
    4. Then implement this method in the .m file of your view controller which you can call

      // Checks if we have an internet connection or not
      - (void)testInternetConnection
      {
          internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];
      
          // Internet is reachable
          internetReachableFoo.reachableBlock = ^(Reachability*reach)
          {
              // Update the UI on the main thread
              dispatch_async(dispatch_get_main_queue(), ^{
                  NSLog(@"Yayyy, we have the interwebs!");
              });
          };
      
          // Internet is not reachable
          internetReachableFoo.unreachableBlock = ^(Reachability*reach)
          {
              // Update the UI on the main thread
              dispatch_async(dispatch_get_main_queue(), ^{
                  NSLog(@"Someone broke the internet :(");
              });
          };
      
          [internetReachableFoo startNotifier];
      }
      

    Important Note: The Reachability class is one of the most used classes in projects so you might run into naming conflicts with other projects. If this happens, you'll have to rename one of the pairs of Reachability.h and Reachability.m files to something else to resolve the issue.

    Note: The domain you use doesn't matter. It's just testing for a gateway to any domain.