How to get change in network connection notification from iOS Reachability Class?
Asked Answered
O

5

13

Hi I want to capture whenever user gets a network connectivity in my application for this I have added apples Reachability class and below is the snippet I am using in my appDelegate class didFinishLaunchingWithOptions method,

Reachability* reachability = [Reachability reachabilityForInternetConnection];
        [reachability startNotifier];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];

and my reachabilityChanged selector method is as below

- (void)reachabilityChanged:(NSNotification*)notification
{
    Reachability* reachability = notification.object;
    if(reachability.currentReachabilityStatus == NotReachable)
        NSLog(@"Internet off");
    else
        NSLog(@"Internet on");
}

but here I am not getting any kind of notification when I switch off my Airplane mode and when I get a network connectivity in my phone.

Am I missing anything?

Oneil answered 23/7, 2013 at 8:15 Comment(1)
Check this https://mcmap.net/q/905223/-reachability-notification-never-called. This will really help you.Baillieu
G
15

I use a variable in appdelegate to store the current network status as a bool

@property (nonatomic, assign) BOOL hasInet;

.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self setUpRechability];
}


-(void)setUpRechability
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNetworkChange:) name:kReachabilityChangedNotification object:nil];

    reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];

    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if          (remoteHostStatus == NotReachable)      {NSLog(@"no");      self.hasInet-=NO;   }
    else if     (remoteHostStatus == ReachableViaWiFi)  {NSLog(@"wifi");    self.hasInet-=YES;  }
    else if     (remoteHostStatus == ReachableViaWWAN)  {NSLog(@"cell");    self.hasInet-=YES;  }

}

- (void) handleNetworkChange:(NSNotification *)notice
{
    NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus];

    if          (remoteHostStatus == NotReachable)      {NSLog(@"no");      self.hasInet-=NO;   }
    else if     (remoteHostStatus == ReachableViaWiFi)  {NSLog(@"wifi");    self.hasInet-=YES;  }
    else if     (remoteHostStatus == ReachableViaWWAN)  {NSLog(@"cell");    self.hasInet-=YES;  }

//    if (self.hasInet) {
//        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Net avail" message:@"" delegate:self cancelButtonTitle:OK_EN otherButtonTitles:nil, nil];
//        [alert show];
//    }
}
Giveandtake answered 23/7, 2013 at 9:25 Comment(1)
handleNetworkChange called multiple times probably the no of wifi connections available, How to control the sameJerid
T
7

Maybe you should add the observer before startnotifier

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(handleNetworkChange:) name: kReachabilityChangedNotification object: nil];
reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
Trincomalee answered 23/7, 2013 at 9:9 Comment(0)
D
0

You can use this wrapper on reachability to get block based callback whenever there is a change in connectivity status.

GitHub: UHBConnectivityManager

If you are using cocoapods.

pod 'UHBConnectivityManager'
Deangelo answered 25/8, 2016 at 12:51 Comment(0)
T
0

Simplest of ways:

reach = [Reachability reachabilityWithHostName:@"www.google.com"];
 reach.reachableBlock = ^(Reachability *reach){
    NSLog(@"Reachable");
};

reach.unreachableBlock = ^(Reachability *reach){
    NSLog(@"Unreachable");
};
[reach startNotifier];

Everytime there is a change in network,respective blocks will be called.

Torchier answered 18/2, 2017 at 18:26 Comment(0)
S
0

Make reachability as your property instead of local variable. It would work.

Storekeeper answered 9/3, 2017 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.