简体   繁体   中英

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

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?

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM