Checking Network Status in iOS Apps

Check for an active Internet connection with the iOS SDK

Introduction

ios
Most mobile applications need to access the web for some reason (for example, calling a web service that retrieves data to populate the UI). This means that, ideally, these applications will (or should) behave differently when no network connection is available. Apple provides a Reachability class that allows you to check when the device connects and disconnects from some sort of Internet connection. It also lets you determine whether the device is connected to Wifi or 3G, which is good for ensuring you do not use up your users’ mobile data.

Reachability.h

The Reachability class uses the SystemConfiguration framework to monitor the network status of an iOS device. It must be downloaded from the Apple site as it is not included in the iOS SDK. As of the 17/08/2013 Reachability class update (version 3), it makes use of storyboards and ARC (Automatic Reference Counting – memory management for iOS). Yay!

Using Reachability.h

reachability

  • On the build phases settings of your project, add SystemConfiguration.framework under Link Binary with Libraries.
  • Add the downloaded Reachability.h and Reachability.m files to your project.
  • Import Reachability.h in all the classes where you want to check for network status.

Synchronous Reachability

Import the Reachability.h header file into your code and then carry out a “spot-check” to determine whether the network is reachable, and to check if there is a wireless or WWAN connection.

#import "Reachability.h"
...
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus status = [reach currentReachabilityStatus];
...

Use a switch statement to decode the network status.

    NSString *string;
    switch(status) {
        case NotReachable:
            string = @"Not Reachable";
           break;
        case ReachableViaWiFi:
            string = @"Reachable via WiFi";
           break;
        case ReachableViaWWAN:
            string = @"Reachable via WWAN";
           break;
        default:
            string = @"Unknown";
           break;
    }
    return string;
}

Notice that there are three possible network states:

  • NotReachable (no Internet connection),
  • ReachableViaWiFi (Wifi Internet connection),
  • and ReachableViaWWAN (mobile data Internet connection).

Asynchronous Reachability

Your application can be notified of changes in the current network status.

First import the Reachability.h header file into your code. After that, register the class that must monitor the network as an observer for the kReachabilityChangedNotification event. Then create a Reachability instance and start event notification:

#import "Reachability.h"
... 
[[NSNotificationCenter defaultCenter] addObserver: self
  selector: @selector(reachabilityChanged:)
  name: kReachabilityChangedNotification
  object: nil];
Reachability *reach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[reach startNotifier];
...

When the network reachability status changes, the Reachability instance will notify your code of the change by calling the reachabilityChanged: method.

- (void) reachabilityChanged: (NSNotification *)notification {
    Reachability *reach = [notification object];
    if( [reach isKindOfClass: [Reachability class]]) { 1
         NetworkStatus status = [reach currentReachabilityStatus];
        // Insert code here
    }
}

Write a Reply or Comment

Your email address will not be published.