2

I'm using this library: https://github.com/tonymillion/Reachability

The problem is sometimes when I check the following expression: [[[ReachabilityManager sharedManager] reachability] isReachable]; returns NO when should be YES. I know there is internet because i invoke Rest services in the meantime.

Plus, if I execute [Reachability reachabilityWithHostname:@"www.google.com"]; and ask for the method isReachable then works as I expected.

My tests are simple, try on emulator and then plug and unplug the ethernet cable.

@implementation ReachabilityManager

#pragma mark -
#pragma mark Default Manager
+ (ReachabilityManager *)sharedManager {
    static ReachabilityManager *_sharedManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedManager = [[self alloc] init];
    });

    return _sharedManager;
}

#pragma mark -
#pragma mark Memory Management
- (void)dealloc {
    // Stop Notifier
    if (_reachability) {
        [_reachability stopNotifier];
    }
}

#pragma mark -
#pragma mark Class Methods
+ (BOOL)isReachable {
    return [[[ReachabilityManager sharedManager] reachability] isReachable];
}

+ (BOOL)isUnreachable {
    return ![[[ReachabilityManager sharedManager] reachability] isReachable];
}

+ (BOOL)isReachableViaWWAN {
    return [[[ReachabilityManager sharedManager] reachability] isReachableViaWWAN];
}

+ (BOOL)isReachableViaWiFi {
    return [[[ReachabilityManager sharedManager] reachability] isReachableViaWiFi];
}

#pragma mark -
#pragma mark Private Initialization
- (id)init {
    self = [super init];

    if (self) {
        // Initialize Reachability
        self.reachability = [Reachability reachabilityWithHostname:@"www.google.com"];

        // Start Monitoring
        [self.reachability startNotifier];
    }

    return self;
}

@end
Eimantas
  • 48,927
  • 17
  • 132
  • 168
Ricardo
  • 7,921
  • 14
  • 64
  • 111

2 Answers2

0
@property (nonatomic) Reachability *hostReachability;
@property (nonatomic) Reachability *internetReachability;
@property (nonatomic) Reachability *wifiReachability;

Declare one bool,

BOOL _isInternetAvaliable;

Init your reachability,

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

    //hostReachability
    NSString *remoteHostName = @"www.apple.com";
    self.hostReachability = [Reachability reachabilityWithHostName:remoteHostName];
    [self.hostReachability startNotifier];
    [self updateInterfaceWithReachability:self.hostReachability];

    //internetReachability
    self.internetReachability = [Reachability reachabilityForInternetConnection];
    [self.internetReachability startNotifier];
    [self updateInterfaceWithReachability:self.internetReachability];

    //wifiReachability
    self.wifiReachability = [Reachability reachabilityForLocalWiFi];
    [self.wifiReachability startNotifier];
    [self updateInterfaceWithReachability:self.wifiReachability];

}


- (void)updateInterfaceWithReachability:(Reachability *)reachability
{
    if (reachability == self.hostReachability)
    {
        [self configurereachability:reachability];
    }

   if (reachability == self.internetReachability)
   {
       [self configurereachability:reachability];
   }

   if (reachability == self.wifiReachability)
   {
       [self configurereachability:reachability];
   }
}


- (void)configurereachability:(Reachability *)reachability
    {
    NetworkStatus netStatus = [reachability currentReachabilityStatus];
    BOOL connectionRequired = [reachability connectionRequired];

    switch (netStatus)
    {
        case NotReachable:        {
            _isInternetAvaliable = NO;
            break;
        }

        case ReachableViaWWAN:        {
            _isInternetAvaliable = YES;
           break;
        }
        case ReachableViaWiFi:        {
            _isInternetAvaliable = YES;
            break;
        }
    }

    if (connectionRequired)
    {
        _isInternetAvaliable = NO;
    }
}


-(BOOL) isInternetAvailable {
    return _isInternetAvaliable;
}

Now you are ready to call & access internet status,

if([[ReachabilityManager sharedManager] isInternetAvailable]){
  //Internet on
}
Mukesh Lokare
  • 2,159
  • 26
  • 38
  • [[ReachabilityManager sharedManager] isInternetAvailable] always return YES with this code. – Ricardo Feb 13 '17 at 12:53
  • I have checked my code again its working in my project , check that you are not connected to any Wifi or WAN network. – Mukesh Lokare Feb 13 '17 at 13:04
  • Debug `configurereachability:` method, if you are not connected any way to internet `netStatus` should get `NotReachable` & it should have to return NO – Mukesh Lokare Feb 13 '17 at 13:08
  • 1
    I can upload a gist to see my adaption of your code. It looks fine to me but it's wierd why does always return yes. – Ricardo Feb 13 '17 at 14:42
  • I have directly call `initializeReachability` method from appDelegate `didFinishLaunchingWithOptions:` method `[[ReachabilityManager sharedManager] initializeReachability];` – Mukesh Lokare Feb 14 '17 at 04:43
0

Finally I found a reliable solution. https://stackoverflow.com/a/14870229/2139691

lionello
  • 499
  • 9
  • 11
Ricardo
  • 7,921
  • 14
  • 64
  • 111