在iOS 7中使用错误的状态代码来响应可达性
我在iOS 7上遇到了奇怪的问题,我已经testing了与iPad1,2,3和iPhone 4,4等其他设备相同的代码,包括iOS 7在内的不同的iOS组合。
问题:
当我打开飞行模式,我得到可达性通知与状态NotReachable
预期,但紧接着该应用程序接收状态代码ReachableViaWWAN
,这是不是预期的通知。
代码:
+(BOOL)checkReachability { Reachability* internetReachable = [Reachability reachabilityForInternetConnection]; NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; switch (internetStatus) { case NotReachable: { DebugLog(@"The internet is down."); return NO; break; } default: return YES; break; } return YES; }
我在交换机上添加了日志,在飞行模式下返回状态为ReachableViaWWAN
。
可能的解决方法可能是:
在这种情况下为ReachableViaWWAN
添加大小写并检查主机是否可达。 并相应地返回BOOL值。
任何人面临类似的问题 我已经search,但没有发现类似的情况。
提前致谢 !!
我有同样的问题。 解决的办法是检查标志isConnectionRequired 。 该文件说:
WWAN可能可用,但在build立连接之前无法使用。
码
BOOL isServerAvailable; Reachability *reachability = [Reachability reachabilityForInternetConnection]; if ((reachability.isConnectionRequired) || (NotReachable == reachability.currentReachabilityStatus)) { isServerAvailable = NO; } else if((ReachableViaWiFi == reachability.currentReachabilityStatus) || (ReachableViaWWAN == reachability.currentReachabilityStatus)){ isServerAvailable = YES; }
旧的可达性文件不好。 苹果已更新其可访问性文件。
在这里检查。
https://developer.apple.com/Library/ios/samplecode/Reachability/Introduction/Intro.html
在这里下载。
https://developer.apple.com/Library/ios/samplecode/Reachability/Reachability.zip
我遇到这个问题,并在这里find了解决办法 。 基本上由于某种原因,即使在飞机模式下也可能获得ReachableViaWWAN
。 但是,还有另外一个标志将表明是否必须首先build立连接。 这是kSCNetworkReachabilityFlagsConnectionRequired
标志,在Reachability类中有一个很好的帮助方法,叫做connectionRequired
如果您使用Apple提供的Network Link Conditioner工具,可达性类可能会产生奇怪的结果。
- (void)handleReachability:(Reachability *)reachability { NetworkStatus netStatus = [reachability currentReachabilityStatus]; BOOL connectionRequired = [reachability connectionRequired]; NSString* statusString = @""; switch (netStatus) { case NotReachable: { if (connectionRequired) { [TSMessage setDefaultViewController:[UIApplication sharedApplication].keyWindow.rootViewController]; [TSMessage showNotificationWithTitle:NSLocalizedString(@"Something failed", nil) subtitle:NSLocalizedString(@"The internet connection seems to be down. Please check that!", nil) type:TSMessageNotificationTypeError]; } connectionRequired = NO; break; } default: break; } }