我在 IOS 应用程序中使用 Reachability 来确定连接。
继这篇文章 wifi on iphone simulator
如果 wifi 关闭,模拟器的互联网连接将不可用,但手机仍连接到 wifi,因此连接没有改变。这一切都很好并且可以理解,当然我可以在设备本身上进行测试。
但是,我希望处理用户已连接到 wifi 但 wifi 没有互联网连接的错误,如下面的屏幕截图所示。
我通过以下方式使用可达性:
#pragma mark - ()
- (void)monitorReachability {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:ReachabilityChangedNotification object:nil];
self.hostReach = [Reachability reachabilityWithHostname: @"api.parse.com"];
[self.hostReach startNotifier];
self.internetReach = [Reachability reachabilityForInternetConnection];
[self.internetReach startNotifier];
self.wifiReach = [Reachability reachabilityForLocalWiFi];
[self.wifiReach startNotifier];
}
//每当状态发生变化时由 Reachability 调用。 - (void)reachabilityChanged:(NSNotification* )note { 可达性*curReach = (可达性*)[注释对象]; NSParameterAssert([curReach isKindOfClass: [Reachability 类]]); NSLog(@"可达性已更改:%@", curReach); 网络状态 = [curReach currentReachabilityStatus];
}
我正在使用来自 tonymillion 的 github 的 Reachability for ARC:https://github.com/tonymillion/Reachability
有谁知道我如何处理这种连接情况并产生更好的错误?
最佳答案
在 appdelegate 中创建以下方法以在任何类上使用它。
-(BOOL)isHostAvailable
{
//return NO; // force for offline testing
Reachability *hostReach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [hostReach currentReachabilityStatus];
return !(netStatus == NotReachable);
}
关于ios - 在 iPhone 模拟器上使用可达性并且没有 wifi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12278903/