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:

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]

// 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:

 if([[Reachability sharedReachability] remoteHostStatus] == NotReachable)
  // what happens when network/server down: 

  [navigationController pushViewController:self.mySecondViewController


  [navigationController popToViewController:self.myFirstViewController


And that’s it. I hope it helps.

Tagged with:

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

👾 Hey! Looking for the latest in frontend? Twice a week, we'll deliver the freshest frontend news, website inspo, cool code demos, videos and UI animations right to your inbox.

Zero fluff, all quality, to make your Mondays and Thursdays more 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 🙂

  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!