How To detect an internet connection with the iPhone SDK

by Josh Highland on December 22, 2009

reachability alert How To detect an internet connection with the iPhone SDKThere 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
 }
  • digg How To detect an internet connection with the iPhone SDK
  • facebook How To detect an internet connection with the iPhone SDK
  • stumbleupon How To detect an internet connection with the iPhone SDK
  • twitter How To detect an internet connection with the iPhone SDK
  • delicious How To detect an internet connection with the iPhone SDK
  • reddit How To detect an internet connection with the iPhone SDK
  • friendfeed How To detect an internet connection with the iPhone SDK
  • posterous How To detect an internet connection with the iPhone SDK
  • tumblr How To detect an internet connection with the iPhone SDK

{ 6 comments… read them below or add one }

1 Josh Highland January 7, 2010 at 5:39 pm

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

2 Justin January 7, 2010 at 4:34 pm

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.

3 Brent February 25, 2010 at 12:04 pm

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 June 17, 2010 at 4:04 pm

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 Josh Highland June 17, 2010 at 4:56 pm

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.

6 Syed December 13, 2011 at 2:31 am

This code takes 30 seconds to check internet. Request you guys to put other code which check internet with in 10 seconds.

Leave a Comment

Previous post:

Next post: