This feature is not available (in other words still being worked on!) - check back soon!
Apple-Posts-Xcode-3-2-1-Developer-Tools-Download-2
Apple’s Reachability Easy API Usage!
Posted by Ben Winn in Tutorials on June 10th, 2011

So when it comes down to making a solid application for the iPhone, iPod Touch & iPad we need good usage examples of the API’s that are available to us, yes there’s  the documentation website on Apple.com but that’s not really useful if you just want a quick example of the API you are wanting.

So during making the InsanelyTech app I used the Reachability API to check if the end-user has a active WiFi/3G/Edge connection on the device, and no I didn’t go too complex on the checking part I just used the simple method I’ve known since I started using the Reachability API. Which you will see shortly below.

Just remember you will need to download the Reachability header file and implementation file (.h & .m) which you can find at the end of this article for your connivence.

So let’s begin with the header file of our sample app…

reachabilitytestAppDelegate.h

//
//  reachabilitytestAppDelegate.h
//  reachabilitytest
//
//  Created by Ben on 10/06/2011.
//  Copyright 2011 InsanelyTech. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface reachabilitytestAppDelegate : NSObject  {
UIWindow *window;
NSString *internetReachability;
BOOL gotInternet;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

-(BOOL)checkInternet;

@end

When you’ve created your project all you need to do is add to the header file:

  • NSString *internetReachability;
  • BOOL gotInternet;
  • -(BOOL)checkInternet;

Everything else can be left as it is and continue onto the implementation file (.m) and start the main coding bit!

reachabilitytestAppDelegate.m

//
//  reachabilitytestAppDelegate.m
//  reachabilitytest
//
//  Created by Ben on 10/06/2011.
//  Copyright 2011 InsanelyTech. All rights reserved.
//

#import "reachabilitytestAppDelegate.h"

@implementation reachabilitytestAppDelegate

@synthesize window;

#pragma mark -
#pragma mark Checking Internet Connection

-(BOOL)checkInternet
{
NSLog(@"---");
NSLog(@"Testing Internet Connectivity");
Reachability *r = [Reachability reachabilityWithHostName:@"insanelytech.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
BOOL internet;
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
internet = NO;
} else {
internet = YES;
}
return internet;
}

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

[self.window makeKeyAndVisible];

return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
*/
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}

- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
}

#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
}

- (void)dealloc {
[window release];
[super dealloc];
}

@end

Now your probably asking how would I call this function somewhere else let’s say ‘application didFinishLaunchingWithOptions…’, now that’s an easy question so all you need to do is define the BOOL value gotInternet with self checkInternet like this:

gotInternet = [self checkInternet];

But that’s just one line? Correct you need to add more than that to make it a functional callback on the function so here’s an example:

NSLog(@"Checking Internet Connectivity...");
gotInternet = [self checkInternet];

if ( gotInternet == 0)
{
UIAlertView *netError = [[UIAlertView alloc]
initWithTitle:@"Network Error"
message:@"You are currently not connected to the internet!"
delegate:self
cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[netError show];
[netError release];

} else {
NSLog(@"Active Internet Found!");
}

Now the shocking bit, that’s all the coding done…see I told you it was a simple method nothing too complex nothing over the top! But there’s two things to do before this project is complete is add the Reachability files to our project and add an existing Framework to the project in order to use the Reachability API.

Add Reachability to the Project:

You should now have this in your Classes folder:

Now we need to add the SystemConfiguration.framework to our projects Frameworks folder, so right click on ‘Frameworks’ and click on ‘Add Existing Framework’:

Once you’ve done that a list of pre-existing frameworks list will show up and yes you need go hunting for the SystemConfiguration.framework as shown below:

Now just click ‘Add’ when done and your project is now complete and should be functional with the Reachability API added, so that’s about it in this tutorial but hopefully you’ve learned something new or maybe not or just got new view on the API. If you do however get stuck during this tutorial then use the comment system below and either myself or a fellow reader will help you!

Thanks for reading…

[Download Reachability.zip] – This archive contains both Reachability.h & Reachability.m

Tags:
Comments Off

Comments are closed.