5

I understand how I can test internet reachability in my app, but what i need to do is constantly listen for reachability, app wide. So if any point anywhere in the app the connection status changes, I can react.

How would I achieve something like this?

Kara
  • 6,115
  • 16
  • 50
  • 57
Josh Kahane
  • 16,765
  • 45
  • 140
  • 253
  • Reachability project in developer,apple.com – Vinodh Sep 27 '13 at 12:48
  • The code is at [Reachability Sample Project](https://developer.apple.com/library/ios/samplecode/Reachability/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007324). The concepts are discussed in [Design for Variable Network Interface Availability](https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/WhyNetworkingIsHard/WhyNetworkingIsHard.html#//apple_ref/doc/uid/TP40010220-CH13-SW3). – Rob Sep 27 '13 at 13:01

1 Answers1

5

You need to add an observer for reachability change notification :

Firstly import in your class: #import "Reachability.h"

then add observer :

   [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(reachabilityChanged:)
                                                 name:kReachabilityChangedNotification
                                               object:nil];


-(BOOL)reachabilityChanged:(NSNotification*)note
{
    BOOL status =YES;
    NSLog(@"reachabilityChanged");

    Reachability * reach = [note object];

    if([reach isReachable])
    {
        //notificationLabel.text = @"Notification Says Reachable"
        status = YES;
        NSLog(@"NetWork is Available");
    }
    else
    {
        status = NO;
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"You are not connected to the internet" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
    return status;
}
Nishant Tyagi
  • 9,893
  • 3
  • 40
  • 61