iPhone OS SDK – Checking Internet Reachability

16/03/2010

Checking for an internet connection is becoming an important part of each iPhone app, as applications and websites evolve to more a more unified experience, getting info off the internet is very important.
Checking for an internet connection on iPhone can be a somewhat moderately difficult task. Luckily apple released a Reachability sample code, which then makes implementation very easy.
Well, in line with the recent theme of my site, I’m going to show you how I implemented it in my programming experiences lately.
I’m going to run you through I how do this in my apps, with a focus on keeping it simple.

Step 1: Add the reachability class from the Reachability Demo
Download them from the apple sample, or here. Then include them in your Xcode, by dragging the sources into xcode (Copying in is fine).

Step 2: Add the Extra Frameworks
Add the Foundation and SystemConfiguration inbuilt frameworks to your app.
Screen-shot-2010-03-16-at-10.37.42.CaUeN2xcbHVT.jpg
Step 3: Think first then code
iPhone-Empty-View-Implemented-by-Tapku.D6WKcx9GiT7b.jpg
The code is in the next step, but first its important to think where you are going to check for Internet Access. If you only need a particular site to work, change the initial ping to that one site. google.com, finance.yahoo.com etc.
econdly and more importantly, think about where you are going to implement the Reachability test. You will generally only want to do it once, in AppDelegate and then store the outcome as a boolean variable (YES/NO). If there ends up being multiple places in the application where you want to test for internet access, remember to do all the lookup business just once. This will speed up the app and also reduce app owners data transfer per session.
I am going to implement this such that a separate icon appears in the top right of my app MainView if there is no internet. Checkout the Three20 or Tapku empty pages, of how they implement the nice ‘empty’ screen which seems to be invented for iPhone by apple.

Step 4: The easy bit: The Code
I want to implement the reachability test in the appDelegate. First lets include the library in appDelegate.h (the header file).
#import “Reachability.h”

in the @interface of the header:
//Got Internets?
NSString *internetReachability;
BOOL gotInternet;

and in the bottom of the header, before end:

-(BOOL)checkInternet;

In the main of the appDelegate, we add a custom method to handle the work, it does most of the heavy lifting in the Reachability.m file. (Notice how the ping url is finance.yahoo.com. I recommend pointing this to the website you are interested in interacting with.
#pragma mark -
#pragma mark Checking Internet Connection

-(BOOL)checkInternet{
//Test for Internet Connection
NSLog(@“——–“);NSLog(@“Testing Internet Connectivity”);
Reachability *r = [Reachability reachabilityWithHostName:@“finance.yahoo.com];
NetworkStatus internetStatus = [r currentReachabilityStatus];
BOOL internet;
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
internet = NO;
} else {
internet = YES;
}
return internet;
}

This method is then able to be called where-ever, but I want to do it in didFinishLaunchingWithOptions. I use the following:
gotInternet = [self checkInternet]; //Test for Internet, calling the self method

if ( gotInternet == 0) {
//I have no internet
NSLog(@”hey, i have no internets”);
internetIcon.hidden = NO;

} else {
//I do have internet
NSLog(@”hey, i have internets: all systems go”);
}

4. And thats it!

Notice how it now stores the variable accessible class-wide. To check for internet now, just check to see the BOOL output of gotInternet. An extension to this is if there is no internet, show the Tapku empty window instead of something internet input related.

Heres the NSLog output, the second launch of the app I turned wifi on, on the mac.

Hope you found it useful, comments possible below.

View Comments