Friday, September 3, 2010

How To detect an internet connection with the iPhone SDK

December 22, 2009 by Josh Highland  
Filed under Programming, Tech, iPhone, iPhone app dev, tutorials

There are several ways to problematically detect if an iPhone has an internet connection, but most of them cant tell you how the iPhone is connected to the internet.

Apple has sample code to accomplish just this task. The Reachability class is not shipped with the SDK, but is part of a sample project of the same name. http://developer.apple.com/iphone/library/samplecode/Reachability/index.html

Using the reachability class:

Set up:

  • Download the reachability project from Apple
  • Copy Reachability.h and Reachability.m to your project.
  • Add the ‘SystemConfiguration’ framework to your project.

Sample Usage:

Reachability *reachability = [Reachability sharedReachability];
 [reachability setHostName:@"www.JoshHighland.com"];
 NetworkStatus remoteHostStatus = [reachability remoteHostStatus];
 
if(remoteHostStatus == NotReachable) {
 //no internet connection
 }
 else if (remoteHostStatus == ReachableViaWiFiNetwork) {
 //wifi connection found
 }
 else if (remoteHostStatus == ReachableViaCarrierDataNetwork) {
 //EDGE or 3G connection found
 }
Share and Enjoy:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • FriendFeed
  • MySpace
  • Ping.fm
  • StumbleUpon
  • Technorati
  • Tumblr
  • Twitter
  • Yahoo! Bookmarks

Comments

5 Responses to “How To detect an internet connection with the iPhone SDK”
  1. Justin says:

    Why is this so popular? This is not correct. Apple updated the Reachability class to 2.0 (you’re referencing 1.5) which has been corrected. Looking for information about the current version is difficult.

    In the new version, there is no +sharedReachability method.

  2. No, I am referencing the 2.0 version, and the code i am displaying is working in my application.

  3. Brent says:

    I’m writing a corporate app using the iphone for delivery tracking. Is there a way to determine which wifi network I’m connected to? We have both a public and private wifi. However, the webservice is only available via the private wifi. The reachability code will tell me when there’s no connectivity, but I have to rely on the webservice call to fail in order to determine that they are not connected to the right network.

  4. Randy says:

    Josh, your comments are spot-on. i’ve jut tried your snippet with the 3.1.3 Snow Leopard SDk and it works fine.

    i’m new to this aspect of app coding and have a simple question. my app needs to communicate over the Net using NSURL objects. does this work over the Carrier Data Network?

  5. Yeah it will work over the cell signal. The iPhone does the translation for you. If you are doing a large transfer of data you will want to use the reachability class to see if you are connected to WIFI first. For pulling up webpages, or connecting to a REST service, you will be fine. check out ASIHTTPRequest for a good wrapper class for making data requests.

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!