iPhone Snippet - Detecting Network Status

Skill

iPhone Snippet - Detecting Network Status

Posted in:

One of the many useful features of the iPhone is that you are almost always connected to the internet - and a large percentage of iPhone apps take advantage of that fact. So you write a great app, and it accesses the internet and all is right with the world, correct? Sadly, no. Sometimes the internet won't be accessible (maybe there is no cellphone or wi-fi signal, maybe the phone is in airplane mode) - and when that happens, you need to make sure that your application behaves correctly.

Detecting network status accurately is actually pretty easy to do (which is why this is a snippet tutorial) - but only if you know to grab the Reachability class from Apple's iPhone reference library. Thats right, Apple wrote some code for us to use, but it is not actually in the framework (which makes it a bit annoying and not obvious to use). So the first step right off the bat is to grab this code we need, and fortunately, you can just get it from Apple's website (as long as you have a Developer Connection account). You need to grab Reachability.h and Reachability.m and add them to your iPhone project.

You can't just use the class yet, though. The Reachability class depends on a framework that is not by default in an iPhone Xcode project - the SystemConfiguration framework. So we need to add this to our project as well.

Just right click on the frameworks folder in Xcode and choose Add->Existing Frameworks. Then you can browse for the SystemConfiguration folder and choose it:

Now that we can use the Reachability class, detecting network status really couldn't be much easier. Say, for instance, we wanted to write the crazy simple app displayed in the screenshots below:

Press the "Check Network" button, and a UIAlertView appears with the network status. If you couldn't tell, the screenshot above is when there is no network, and the screenshot below is when there is a network connection :P

Below is the code for the method that is hooked up to that "Check Network" button:

#import "SOTC_ReachabilityExampleViewController.h"
#import "Reachability.h"

@implementation SOTC_ReachabilityExampleViewController

- (IBAction)checkNetwork:(id)sender{
 
  UIAlertView *errorView;
 
  if([[Reachability sharedReachability] internetConnectionStatus] == NotReachable) {
    errorView = [[UIAlertView alloc]
                 initWithTitle: @"Network Error"
                 message: @"The internets are down, everyone run for your lives!"
                 delegate: self
                 cancelButtonTitle: @"Nooooooo!" otherButtonTitles: nil];    
  }
  else
  {
    errorView = [[UIAlertView alloc]
                 initWithTitle: @"Whew!"
                 message: @"Relax, the internets are here."
                 delegate: self
                 cancelButtonTitle: @"Yay!" otherButtonTitles: nil];    
  }
 
  [errorView show];
  [errorView autorelease];
}

@end

The Reachability class is made to be used as a singleton, and so calling the static method sharedReachability returns that singleton instance. Once we have that in our hands, we just need to call the method internetConnectionStatus. This will have a return value of one of three things: NotReachable (which is what we check against here, and means that there is no network connection), ReachableViaCarrierDataNetwork (which means we are using EDGE or 3G), or ReachableViaWiFiNetwork (which means that the phone is on wi-fi).

For simple checking of network connection status, there is nothing else you need to do! The Reachability class exposes some other handy methods as well - you can easily check the reachability of a particular remote host using the remoteHostStatus method. You can even hook up to be notified of network status changes through the ReachabilityCallback.

Well, that is it for this snippet tutorial on network status (and the first tutorial during SOTC's Month of the iPhone). You can grab the Xcode project for the simple example we made today in the zip file below, and feel free to drop a comment if you have any questions.

Anonymous
11/28/2009 - 22:20

Doesn't work on my tabbar application... can you email me? minichrispy@gmail.com

reply

Add Comment

Put code snippets inside language tags:
[language] [/language]

Examples:
[javascript] [/javascript]
[actionscript] [/actionscript]
[csharp] [/csharp]

See here for supported languages.

Javascript must be enabled to submit anonymous comments - or you can login.

Sponsors