How to force checking internet reachability using Reachability.m/.h

378 views Asked by At

I'm using Reachability.m/.h to check internet/wifi status. Everything is working great with the Library, I get a notification every time the status changed thanks to the notifier and the observer but sometimes (rarely but still, sometimes) the status doesn't change.

In some part of my code I need to "force" the checking of the reachability. Is there any way I can do that with the class Reachability.m/.h?

1

There are 1 answers

3
samouray On

Create a method named connected :

- (BOOL)connected
{
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [reachability currentReachabilityStatus];
    return networkStatus != NotReachable;
}

Use it like this when you want to force check the reachability :

if (![self connected]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No connection" message:@"you have to be connected in order to continue" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alert show];
        NSLog(@"no connection");
    } else {
// the user is connected , write your code here
}