Check internet Connection in iPhone apps

Here I am going to explain you how we can check internet connectivity in iPhone applications.

I have used reachability sample application provided from Apple. You can grab the sample code of Reachbility application from Apple’s developer website. After that just folow below listed steps to make it working in your application.

  1. Add the SystemConfiguration.framework to your project by going in the Edit Project.
  2. Add the files Reachability.h and Reachability.m from the Reachability sample code to your project.
  3. Add the following code in the implementation file where you need to test the Internet connection.
///Import at the beginning of your implementation file
#import "Reachability.h"

//// Where you need it
Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];

if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)){
  /// Create an alert if connection doesn't work
  UIAlertView *myAlert = [[UIAlertView alloc]
          initWithTitle:@"No Internet Connection"
          message:@"You require an internet connection via WiFi or cellular network for location finding to work."
          delegate:self
          cancelButtonTitle:@"Ok"
          otherButtonTitles:nil];
  [myAlert show];
  [myAlert release];
} else{
  /// Whatever you want
}