~If you would like to request a tutorial, email Collateral at collateraldamag3@gmail.com~

Share This!

iPhone Tutorial ~ Reachability and Checking Network Status

in



This is a tutorial on the Reachability class and checking your network status. Apple's Human Interface Guidelines for the App Store require any application that uses a network connection to check to see if that connection is active and if not, display an alert telling the user. Well, i spent a good 2 days trying to figure this one out, and hopefully with this tutorial, you'll be able to do it in 10 minutes. 
The first thing that is tricky about this is that the Reachability class that is used for this ISN'T EVEN INCLUDED IN THE SDK! WTF APPLE?!? The Class is actually found in one of Apple's sample code applications with the same name. I have downloaded that sample and extracted the Reachability class from it for you to download, here:
DOWNLOAD - REACHABILITY CLASS
You can download Apple's original sample code application in its entirety from HERE.


Unzip the Reachability class and add the Reachability.h/m to your Xcode project. This class also requires use of the SystemConfiguration Framework. So right-click on the "Frameworks" folder, go to "Add" and then "Existing Frameworks". 



Then click on "SystemConfiguration.framework". 


Alright, You have now successfully added the Reachability class to your project. Now how do we utilize it?
Well what we are going to do is check for network connectivity, and if there is none, alert the user. If there is, we will use NSLog to log in the console how the user is connected. 
First, declare a method: - (void)networkCheck; in your .h file.
Then, move to your .m file and fill in the method:



-(void)networkCheck{
Reachability *curReach = [[Reachability reachabilityForInternetConnection] retain];
NetworkStatus netStatus = [curReach currentReachabilityStatus];
switch (netStatus)
{
case NotReachable:
{
UIAlertView *connectionAlert = [[UIAlertView alloc] init];
[connectionAlert setTitle:@"Error"];
[connectionAlert setMessage:@"myApp was not able to reach the host. Please check your network conenction."];    
[connectionAlert setDelegate:self];
[connectionAlert setTag:1];
[connectionAlert addButtonWithTitle:@"Back"];
[connectionAlert show];
[connectionAlert release];
NSLog(@"NETWORKCHECK: Not Connected");
break;
}
case ReachableViaWWAN:
{
NSLog(@"NETWORKCHECK: Connected Via WWAN");
break;
}
case ReachableViaWiFi:
{
NSLog(@"NETWORKCHECK: Connected Via WiFi");
break;
}
}
}



First  we are declaring *curReach as our current network connectivity. We are then assigning that to a NetworkStatus object (Thanks to your Reachability Class) and preforming a switch-case on it. If it is "Not Reachable", we alert the user. If it connects, we log to the console by which method the connection is being preformed. 


Thats it :) That should get you set for compliance with Apple's HIG's. See ya next time.
~Collateral

No comments:

Post a Comment

This entry is filed under .

You can also follow any responses to all entry through the RSS Comments feed.