Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constantly Listen for Internet Reachability?

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?

like image 752
Josh Kahane Avatar asked Sep 27 '13 12:09

Josh Kahane


1 Answers

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;
}
like image 162
Nishant Tyagi Avatar answered Sep 30 '22 10:09

Nishant Tyagi