Implementation of notification service in iPhone

Today I will explain you how we can implement notification service in the iPhone application.

In my previous post i had tried to distinguish how one can use Reachability example provided from Apple sample code to check internet connection availability in their application. Now it is time to get network change notification in our iPhone app. AKA when ever network connectivity state will be changed we will get the update notification from iOS in our application.

You just need to do few lines of code for making this happen in your application. so let’s start it step by step.

  1. Follow first two steps from my last post regarding checking internet connection in iPhone.
  2. Add below listed code in your ApplicationAppDelegate.m file in the function where I have shown in the code.
///Import at the beginning of your implementation file
#import "Reachability.h"

//// Where you need it
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the
    // method "reachabilityChanged" will be called.
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];

    r = [[Reachability reachabilityWithHostName:kReachableDomain] retain];

    [r startNotifier];

    // Add the view controller's view to the window and display.
    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];
    [viewController showSplash];

    return YES;
}
  1. Now we have gone through half of the way, we have registered notification handler using above code. Please notice the above code, we assigned a “reachabilityChanged:” function as a notification receive handler. So we must implement it in same application delegate class.
  2. To implement NSNotification receiver add below listed code in your class file.
//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note
{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);

    NetworkStatus internetStatus = [curReach currentReachabilityStatus];

    if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
    {
        UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"You require an internet connection via WiFi or cellular network for getting data from website" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [myAlert show];
        [myAlert release];
        //[self applicationDidEnterBackground:application];
    }

    [curReach release];
}
  1. That’s it. Now when ever user change the internet connection type or disable it then your application will get notification of it.

I hope you enjoyed the way we have implemented NSNotification for checking internet connectivity.

Check internet Connection in iPhone apps

Here I am going to explain you how we can check internet connectivity in iPhone applications.

I have used reachability sample application provided from Apple. You can grab the sample code of Reachbility application from Apple’s developer website. After that just folow below listed steps to make it working in your application.

  1. Add the SystemConfiguration.framework to your project by going in the Edit Project.
  2. Add the files Reachability.h and Reachability.m from the Reachability sample code to your project.
  3. Add the following code in the implementation file where you need to test the Internet connection.
///Import at the beginning of your implementation file
#import "Reachability.h"

//// Where you need it
Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];

if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)){
  /// Create an alert if connection doesn't work
  UIAlertView *myAlert = [[UIAlertView alloc]
          initWithTitle:@"No Internet Connection"
          message:@"You require an internet connection via WiFi or cellular network for location finding to work."
          delegate:self
          cancelButtonTitle:@"Ok"
          otherButtonTitles:nil];
  [myAlert show];
  [myAlert release];
} else{
  /// Whatever you want
}