【问题标题】:wrong status for Reachability in appDidBecomeActiveappDidBecomeActive 中可达性的错误状态
【发布时间】:2014-10-31 13:05:27
【问题描述】:

我正在使用 Apple 提供的 Reachability 类,但遇到了一件奇怪的事情。 每次应用程序确实处于活动状态时,应用程序都会检查连接,如果它处于活动状态,请更新一些数据。 当我打开飞行模式并重新启动应用程序后,将调用 didBecomeActive,可达性返回错误状态(reachableViaWiFi)。如果你再重复一次,就会返回正确的状态。

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

这种行为有什么解释吗?

【问题讨论】:

    标签: ios reachability


    【解决方案1】:

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

    检查以下条件:

    - (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;
            }
        }
    }
    

    【讨论】:

    • 第一个和第二个switch语句有什么区别?两者似乎都在检查 [reachability currentReachabilityStatus]
    • @sooper:谢谢伙计 :) 我确实错过了那一点,编辑了答案。
    猜你喜欢
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-30
    • 2013-05-01
    相关资源
    最近更新 更多