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

时间:2015-06-23 13:59:18

标签: ios iphone swift reachability network-connection

我正在使用Reachability.m / .h来检查互联网/ wifi状态。一切都在图书馆工作得很好,每次状态改变都会收到通知,这要归功于通知者和观察者,但有时(很少但有时仍然)状态不会改变。

在我的代码的某些部分,我需要“强制”检查可达性。有什么方法可以用Reachability.m / .h?

这个类来做

1 个答案:

答案 0 :(得分:0)

创建名为connected的方法:

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

当你想强制检查可达性时,请使用它:

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
}