简体   繁体   English

如何使用Reachability.m / .h强制检查Internet可达性

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

I'm using Reachability.m/.h to check internet/wifi status. 我正在使用Reachability.m / .h检查互联网/ WiFi状态。 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? 我可以通过类Reachability.m / .h来做到这一点吗?

Create a method named connected : 创建一个名为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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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