I figured I would put together a simple tutorial since I’ve been getting a lot of traffic based on “iPhone Human Interface Guidelines.” This is probably due to one of my earlier blog post, “Developing My First iPhone App.” I failed to adhere to the guidelines in the beginning, so my app was originally rejected. Here is a simple tutorial so you don’t have the same issue as I did.
Download the Reachability project from Apple. The link to the project is:
http://developer.apple.com/iphone/library/samplecode/Reachability/index.html
The two files we will need are: Reachability.h and Reachability.m
Add SystemConiguration Framework:
1. Control click (right click) on the Frameworks folder and select Add then select Existing Frameworks.
2. Scroll down and select SystemConfiguration.framework.
3. Click Add.
4. Control click (right click) on the project icon and select Add then select Existing Files.
5. Find the Classes folder in the Reachability project, and select the files Reachability.h and Reachability.m.
6. Click Add, then make sure the check box is selected for “Copy items into destination group’s folder (if needed).”
7. Click Add.
Inside ViewController.h file, add the following statement to the top of the header file.
#import "Reachability.h"
Inside the interface definition of the view controller, add the variable definitions:
NSString *urlAddress;
NSURL *url;
NSURLRequest *requestObj;
NetworkStatus remoteHostStatus;
NetworkStatus internetConnectionStatus;
Add the following properties:
@property (nonatomic, retain) NSString *urlAddress;
@property (nonatomic, retain) NSURL *url;
@property (nonatomic, retain) NSURLRequest *requestObj;
@property NetworkStatus remoteHostStatus;
@property NetworkStatus internetConnectionStatus;
Inside ViewController.m file add the synthesize statements after the implementation statement:
@synthesize webView, urlAddress, url, requestObj;
@synthesize remoteHostStatus, internetConnectionStatus;
Inside the viewDidLoad method, add the following lines of code to set the variables:
[[Reachability sharedReachability] setAddress:@"http://www.apple.com"];
[[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];
Inside the programming logic, use the following lines of code to test network connectivity. Substitute the code you would like to execute where the “NSLog(@”Connection Made”)” statement is.
// Query the SystemConfiguration framework for the state of the device's network connections.
self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];
self.internetConnectionStatus = [[Reachability sharedReachability] internetConnectionStatus];
// Check if there is a connection
if (self.internetConnectionStatus == NotReachable && self.remoteHostStatus == NotReachable)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error Loading"
message:@"No Internet Connection"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
} else {
NSLog(@"Connection Made");
}
Don’t forget to clean up and add the following statements to the dealloc method.
[urlAddress release];
[url release];
[requestObj release];


#1 by Oliver Drobnik on January 16th, 2010
Quote
Of course you would want to check availability not for 127.0.0.1 but a real address. Localhost is aways reachable.
#2 by nick on January 16th, 2010
Quote
Thanks for the catch! I just fixed and updated the post.