0

Hi I am using Reachability to detect wether my application is online or offline. I want to implement that, while running and suddenly my internet connection lost then i want to show al alert.

I tried with following code to do the same:

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

- (void)reachabilityDidChange:(NSNotification *)notification {
    //  Reachability *reachability = (Reachability *)[notification object];

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

    if (reachability == ReachableViaWiFi) {
        NSLog(@"Reachable");
    } else {
        NSLog(@"Unreachable");
    }
}
Rakesh
  • 1,177
  • 1
  • 15
  • 31
  • Did it work? - if not what was the error? - What else have you tried? – Wez Apr 14 '15 at 08:38
  • The implementation is fine, the question is where are you doing this, it should be added in class like AppDelegate so that you have constant monitoring. – iphonic Apr 14 '15 at 08:41
  • @Wezly Thank you for quick response. Its not working for me. I put a break point in that method and tries to turn on and off WiFi. The method is not getting called at all. – Rakesh Apr 14 '15 at 08:41
  • @iphonic i implemented this in appDelegate. Its not working. – Rakesh Apr 14 '15 at 08:42
  • Have a look at [this](http://stackoverflow.com/a/13247360/757214) answer. – Wez Apr 14 '15 at 08:45
  • I used that code. But my notification change selector should call. – Rakesh Apr 14 '15 at 08:48
  • 1
    Try to put the [[Reachability reachabilityForInternetConnection] startNotifier] method to the app delegate, without that the Reachability class won't notify you about the network change. The rest of the code is ok. – Marco Pace Apr 14 '15 at 08:48
  • You are calling `[reachability startNotifier];` in wrong place, it should be outside where you have added the notification delegate – iphonic Apr 14 '15 at 09:06

0 Answers0