How To Test For An Internet Connection On The iPhone

If you write an app that uses the internet then the Apple Human Interface Guidelines require that you check for a WiFi or Wireless Wide Area Network (cellular) connection and let the user know if they are not available.

nofunctionscreen

iTunes app with no WiFi

One reliable and easy way to test for this is to make use of the code in the Reachability sample project that Apple provides with XCode. Here is what you need to do:

  1. Add Reachability.h and Reachability.m from the sample project to your project
  2. Add the SystemConfiguration.framework to your project
  3. Use the following code in your project:
[[Reachability sharedReachability] setHostName:@"www.apple.com"];
NetworkStatus internetStatus = [[Reachability sharedReachability] remoteHostStatus];
if ((internetStatus != ReachableViaWiFiNetwork) && (internetStatus != ReachableViaCarrierDataNetwork)) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection"
        message:@"An internet connection via WiFi or cellular network is needed to use this app."
        delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [alert show];
    [alert release];
} else {
    // do whatever your program does
}

In the first line you can set setHostName: to whatever you like. A simple page on your server that just returns True works just fine.

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Technorati
  • LinkedIn
  • RSS
  • Slashdot
  • Twitter

About Jason Job

I am a technologist, music producer and an obsessive student of the Internet. I create sound, write code and make things with electronics and microcontrollers. I work as a software developer and consultant and am currently focused primarily on mobile applications for iPhone, iPad and iPod Touch.
This entry was posted in code, iPhone. Bookmark the permalink.

2 Responses to How To Test For An Internet Connection On The iPhone

  1. David Dietz says:

    That’s great and all, but being a noob, I could use a little more direction here. In step 3 you said, “add this code.” But you failed to tell us where to add that code. Does it go in the h, m, or systemconfiguration files? Also, at the end of the sample code, I could use some help telling the Reachability app what “else” to do. else die? else load app? else WHAT?

  2. jj0b says:

    The code I provided should be placed in your implementation file (the .m one). I have been using this code in my ViewWillAppear method.

    As far as what ‘else’ to do, that depends on your app. If your app connects to the internet and downloads a picture then the ‘else’ statement in my example is where you would do that. That is a super simple example of course. Basically the rest of your implementation file that you want to execute if there is an internet connection would go there.

    I put the Reachability.h and Reachability.m in my Classes folder and add the SystemConfiguration.framework by right-clicking (or command-clicking) on the Frameworks folder and choosing Add->Existing Frameworks and choosing the framework.

    Hope that helps clarify things.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>