Domanda

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?

È stato utile?

Soluzione

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;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top