我正在使用Reachability.m / .h来检查互联网/ wifi状态。一切都在图书馆工作得很好,每次状态改变都会收到通知,这要归功于通知者和观察者,但有时(很少但有时仍然)状态不会改变。
在我的代码的某些部分,我需要“强制”检查可达性。有什么方法可以用Reachability.m / .h?
这个类来做答案 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
}