How to Create an iOS App
How to Use a Navigation Controller in the Master View for Your iOS App
How to Add a New View Controller to Your iOS App

How to Check Your iOS App for Network Availability

One of the easiest ways to get your app rejected by Apple is to fail to make sure that you have an Internet connection when your app needs it, and therefore fail to notify the user that the functionality that requires the connection will be unavailable (or even worse, have your app just hang there).

Downloading the Reachability sample

Apple provides a sample app called Reachability that shows how to determine whether you have an Internet connection. Here's how to use code from that valuable sample program:

  1. Download the Reachability sample from Apple by clicking Sample Code.

  2. Type Reachability in the Search field.

  3. Click the Reachability project in the search results, and in the iOS Developer Library window that appears, click the Download Sample Code button.

  4. In your Downloads folder, double-click the Reachability folder to open it.

    You set your Safari Downloads folder in Safari→Preferences using the General tab.

  5. Open the inner Reachability folder and drag the Reachability.h and Reachability.m files into your project.

  6. Select the check box in front of Road Trip in the Add to Targets section.

    Be sure to select the Copy Items into destination group’s folder option (if it isn't already selected).

In order for you to be able to use this code, you need to add the SystemConfiguration framework. To do so, follow these steps:

  1. In the Project navigator, select the project icon (in this case, RoadTrip) at the top of the Project navigator content area to display the Project editor.

  2. In the targets pop-up menu just below the jump bar for the project, select RoadTrip.

  3. On the Build Phases tab, scroll down to the Link Binary with Libraries section.

  4. Expand the Link Binary with Libraries section if it isn't already expanded by clicking the disclosure triangle.

    image0.jpg
  5. Click the + (plus sign) button underneath the list of current project frameworks.

    A list of frameworks appears.

  6. Scroll down and select SystemConfiguration.framework.

    image1.jpg
  7. Click the Add button.

    You’ll see the framework added to the Linked Frameworks and Libraries section.

  8. Close the Linked Frameworks and Libraries section.

Adding the code to check for reachability

The place to check for whether you have access to the Internet is right when you start up. The method for doing that is the app delegate protocol method application:didFinishLaunchingWithOptions:.

You also need to include the Reachability.h file to be able to use Reachability, so add the bolded code in Listing 8-1 to the beginning of both the AppDelegate.m file and the application:didFinishLaunchingWithOptions: method.

#import "AppDelegate.h"
#import "Reachability.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
   didFinishLaunchingWithOptions:
       (NSDictionary *)launchOptions
{
 if ([[UIDevice currentDevice] userInterfaceIdiom] ==
        UIUserInterfaceIdiomPad) {
  UISplitViewController *splitViewController = (UISplitViewController *)
       self.window.rootViewController;
  UINavigationController *navigationController =
   [splitViewController.viewControllers lastObject];
  splitViewController.delegate =
    (id)navigationController.topViewController;
 }
NetworkStatus networkStatus =
 [[Reachability reachabilityForInternetConnection]
        currentReachabilityStatus];
 if (networkStatus == NotReachable) {
 UIAlertView *alert = [[UIAlertView alloc]
  initWithTitle:@"Network Unavailable"
  message:@"RoadTrip requires an Internet connection"
  delegate:nil
  cancelButtonTitle:@"OK"
  otherButtonTitles:nil];
 [alert show];
 }
 return YES;
}

Ignore the code not in bold for the time being.

In the main bolded section, you start by creating a Reachability object and then send it the currentReachabilityStatus message:

NetworkStatus networkStatus =
 [[Reachability reachabilityForInternetConnection]
        currentReachabilityStatus];

reachabilityForInternetConnection is an initializer that creates a Reachability object that checks for the availability of an Internet connection. Reachability has a lot of functionality, but all you really care about right now is whether you can reach the Internet.

Next, check to see whether you have network access:

if (networkStatus == NotReachable) {

If you don’t have network access, you post an alert:

UIAlertView *alert = [[UIAlertView alloc]
  initWithTitle:@"Network Unavailable"
  message:@"RoadTrip requires an Internet connection"
  delegate:nil
  cancelButtonTitle:@"OK"
  otherButtonTitles:nil];
[alert show];

This is the standard way to configure and then show an alert. You have filled in the various (self-explanatory) parameters required by the initialization method. Configured this way, the alert will have a single button.

The show message to the alert object causes the alert to be displayed in the window, and when the user taps OK, the alert is dismissed.

If you had added other buttons to give the user a choice of responses, you would have had to make the object posting the alert (the AppDelegate, in this case) a UIAlertViewDelegate, assigned the delegate parameter to self, and added the title of the other buttons using a nil terminated list. You would then have needed to implement the alertView:clickedButtonAtIndex: method in the delegate.

By examining the code, you can easily figure out how to get any other network status information you want.

If you run the app now, and either turn off your Internet connection on the computer (if you're running the Simulator) or turn on Airplane mode or turn off your Wi-Fi connection on the device, you see this message. (What matters isn’t the behavior on the simulator but, later on, the behavior on a device.)

image2.jpg
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Add the Weather View Controller to Your iOS App
How to Add a Button to Your iOS App
How to Use the Library Pane for iOS App Development
How to Create an Outlet for the Map Controller in Your iOS App
How to Add a Toolbar to Your iOS App
Advertisement

Inside Dummies.com