Test if your iPhone app is connected to internet

Posted by: Idriss

Here’s a simple way to test if your iPhone application is connected using Wifi, 3G or Edge.

First go to the Developer documentation and search for “Reachability” (or here: Online Apple Doc: Reachability ): Apple provided this class to simplify the way to monitor the network of an iPhone or iPod Touch.

Open the project and copy these files to your project: Reachability.m and Reachability.h.

This class relies on the “SystemConfiguration” Framwork, so you need to add it to your project:

  • Control+click on your Framework folder in X-code
  • Choose Add> Existing Frameworks…
  • Search for “SystemConfiguration.Framework” and add it

Now you can use the Reachability class somewhere in your code like that:

if([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == NotReachable) {
	UIAlertView *error = [[UIAlertView alloc]
			initWithTitle: @"No Internets??"
			message: @"Check your Wifi settings"
			delegate: self
			cancelButtonTitle: @"Ok, i'll do it" otherButtonTitles: nil];
		[error show];
}

Of Course you can change the test to something else:

  • ReachableViaWWan : means WIFI or 3G
  • ReachableViaWifi: means only WIFI
  • NotReachable: no connection at all

Simple!