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 }














{ 6 comments… read them below or add one }
No, I am referencing the 2.0 version, and the code i am displaying is working in my application.
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.
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.
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?
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.
This code takes 30 seconds to check internet. Request you guys to put other code which check internet with in 10 seconds.