appDidBecomeActive中可达性的错误状态

我正在使用苹果提供的Reachability类,并面临一件奇怪的事情。 应用程序在每次应用程序激活时检查连接,如果它处于活动状态,则更新一些数据。 当我打开飞行模式,然后重新启动应用程序,所以didBecomeActive将被调用,可达性返回错误状态(reachableViaWiFi)。 如果您再重复一次,则返回正确的状态。

另外我注意到,如果你打开飞行模式,等待几秒钟,然后重新启动应用程序,可达性返回正确的状态。

这种行为有没有解释?

当您检查连接时,您需要更严格。 在收到可达性更改通知时添加更多条件。

检查以下条件:

- (void)reachabilityDidChange:(NSNotification *)notification { // Reachbility for internet Reachability *reachability = (Reachability *)[notification object]; NetworkStatus internetStatus = [reachability currentReachabilityStatus]; switch (internetStatus) { case NotReachable: { NSLog(@"The internet is down."); break; } case ReachableViaWiFi: { NSLog(@"The internet is working via WIFI."); break; } case ReachableViaWWAN: { NSLog(@"The internet is working via WWAN."); break; } } // Reachbility for host Reachability *hostReachability = [Reachability reachabilityWithHostName:@"www.apple.com"]; NetworkStatus hostStatus = [hostReachability currentReachabilityStatus]; switch (hostStatus) { case NotReachable: { NSLog(@"A gateway to the host server is down."); break; } case ReachableViaWiFi: { NSLog(@"A gateway to the host server is working via WIFI."); break; } case ReachableViaWWAN: { NSLog(@"A gateway to the host server is working via WWAN."); break; } } }