0

I am trying Tony's Reachability, below is the standard sample code:

  Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];

  reach.reachableBlock = ^(Reachability*reach)
  {
    dispatch_async(dispatch_get_main_queue(), ^{
      NSLog(@"reachable!");
    });
  };

  reach.unreachableBlock = ^(Reachability*reach)
  {
    dispatch_async(dispatch_get_main_queue(), ^{
      NSLog(@"unreachable!");
    });
  };

  [reach startNotifier];

I am using emulator and tried to turn off mac's wifi, it prints the unreachable, then turns on mac's wifi but it intermittently doesn't print the reachable, in condition: application is still in use (foreground).

Is there any way to solve this?

Rendy
  • 5,311
  • 11
  • 45
  • 86

2 Answers2

0

This is what I did on my end and got the job done for me. I try using the blocks but it seems it was more trouble that solving my issue. Hope this helps.

Reachability *reach = [Reachability reachabilityWithHostname:@"www.google.com"];
if ([reach isReachable]){
    // Reachable
    NSLog(@"reachable");
}else{
    // not Reachable
     NSLog(@"unreachable!");
}

if you try only reachableBlock then check this link for your help

Community
  • 1
  • 1
Lalji
  • 695
  • 1
  • 6
  • 19
0

Because reachability blocks are called asynchronously on a background thread, you won't always be notified immediately. The purpose of the docs using the main thread like in your example, is because UI updates need to be performed on the main thread.

In short, it's behaving as expected.

brandonscript
  • 57,554
  • 29
  • 142
  • 204