Search code examples
iosreachability

iOS Reachability not working


I'm using the popular Reachability framework (https://github.com/tonymillion/Reachability) in my app and I'm testing reachability on a real device (though it's exactly the same behavior on simulator too).

Here is my code (I'm strongly referencing the Reachability instance as an instance variable in my app delegate):

@implementation AppDelegate{
    Reachability *reachability;
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [...]

    //this code is reached and called with no issues.

    reachability = [Reachability reachabilityWithAddress:@"myreachabledomain.com"];
    [reachability startNotifier];
    return YES;
}

In somewhere else (again, strongly referenced):

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

[...]

-(void)reachabilityDidChange:(NSNotification*)note{
    NSLog(@"hello");
}

However, I never get to see that "hello" (or hit the breakpoint). My domain is perfectly accessible. I toggle my phone's internet connectivity by permanently setting cellular data off, then running my app and toggling WiFi (yep, it connects to WiFi without issues and I can browse the internet).

What am I doing wrong? (I'm on iPhone 7 Plus, iOS 10.3.1)

UPDATE:

  • I've tried google.com instead of my custom domain, no avail.
  • I've tried registering for notification before starting listening, no avail.
  • I've tried using the block-based methods instead of notification center, no avail.

Solution

  • I'm a bit late to the party but I just tried and was having the same issue.

    I tried using [Reachability reachabilityWithHostName:@"google.com"]; instead of reachabilityWithAddress and it works as expected.

    I can't explain why this works right now but hopefully you can use this option.