Checking Network Reachability on the iPhone

In some iPhone apps you need to check if there is a connection to the outside world. If the application depends on a connection to the internet, we will want […]

In some iPhone apps you need to check if there is a connection to the outside world. If the application depends on a connection to the internet, we will want to warn the user of the app, if he cannot connect currently. In programming your app you can for example make sure that a certain view is pushed whenever there is no connection available or when your server that you need to connect to is down. When the connection is back up, you can again remove that view or show the default view of your app.

I want to show a simple way how to get notified using the reachability class. If you would like to see how to extensively use the class in a project setting, check out this elaborate tutorial:

http://www.raddonline.com/blogs/geek-journal/iphone-sdk-testing-network-reachability/

Here is a checklist for integrating an observer for the connection:

  1. Get the Reachability class from Apple, it has all the functionality we need to check if a connection is up/down
  2. Import the class into your app
  3. Initialize it and create an observer
  4. Create a method that handles an alteration of the network status

Basically, we will subscribe to the Notification Center or kNetworkReachabilityChangedNotification which will send us notifications when a change happens. We will handle that change by checking the content of the message sent.

After you get the Reachability class from Apple, import it in your AppDelegate. There we will do our initialization and add an observer. As well, we will put our method there.

So, we are in your YourAppDelegate.m and import the Reachability class:

#import "Reachability.h"

In the applicationDidFinishLaunching Method we will do the initialization and add an observer:

// Set the host name to check:

[[Reachability sharedReachability]
     setHostName:@"www.yourserver.com"]];

// Enable the status notifications:

[Reachability sharedReachability].networkStatusNotificationsEnabled = YES; 

// Add an observer to the notification center and call the method
// handleReachability when there was a notification:

[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(handleReachability: )
    name:@"kNetworkReachabilityChangedNotification" object:nil];

I am assuming that you defined some navigation controller and your view controllers. So, we want to get notified whenever the reachability changed. We will call the method handleReachability when that happens:

-(void)handleReachability:(NSNotificationCenter*)notification{
 if([[Reachability sharedReachability] remoteHostStatus] == NotReachable)
 {
  // what happens when network/server down: 

  [navigationController pushViewController:self.mySecondViewController
   animated:YES];

 }else{

  [navigationController popToViewController:self.myFirstViewController
   animated:YES];

 }
}

And that’s it. I hope it helps.
Enjoy!

Tiny break: 📬 Want to stay up to date with frontend and trends in web design? Subscribe and get our Collective newsletter twice a tweek.

Manoela Ilic

Manoela is the main tinkerer at Codrops. With a background in coding and passion for all things design, she creates web experiments and keeps frontend professionals informed about the latest trends.

Stay in the loop: Get your dose of frontend twice a week

Fresh news, inspo, code demos, and UI animations—zero fluff, all quality. Make your Mondays and Thursdays creative!

Feedback 5

Comments are closed.
  1. not working with the new Reachability Class, and I don’t want to use the old Class from the referenced Tutorial.

  2. Hi,
    I’ll look into that. It might need just a little change to work with the new one…

  3. Hi, did you checked with the new Class?? I really would appreaciate a small Source you could make for us beginners, we can download .. best including a popup that shows “Internet not available” or so 🙂
    thx
    chris

  4. Hi, have you had a chance to look at Reachability 2.0? I’m looking for a simple solution using a notification center and I’m still learning.

    • Hi Taco,
      I am really sorry but I trashed my Mac and cannot develop for the iPhone these days!
      That’s why I haven’t been posting in this category any more. I will write more tutorials once I have a new Mac!
      Greetings,
      ML