iphone - Apple 的 Reachability 源代码中存在严重错误? (本地 WiFi 的可达性)

标签 iphone ios

我想将网络状态通知集成到我的项目中,并为此使用了 Apple 的 Reachability 类。尽管如此,我可能在他们的代码中发现了一个错误,或者也可能是由模拟器本身引起的。

代码在这里:

- (void)start {

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateStatus:) 
                                                 name:kReachabilityChangedNotification 
                                               object:nil];

    Reachability *wifi = [[Reachability reachabilityForLocalWiFi] retain];
    [wifi startNotifier];

}

- (void)updateStatus:(NSNotification *)notice {
    NetworkStatus s = [[notice object] currentReachabilityStatus];
    if(s == NotReachable) {
        NSLog(@"Wifi not reachable");
    } else {
        NSLog(@"Wifi is reachable");
    }
}

现在,当调用“start”时会发生什么:

1) updateStatus 消息没有被触发 - 好吧,可能不是一个错误,也许这是正常行为

2) 当我关闭 Mac 的 Airport 且网络状态为 eq 时,会触发 updateStatus 消息。到“NotReachable”,但是当我再次打开 Mac 的 Airport 时,将触发 updateStatus 消息并且网络状态保持“NotReachable”

当我在启动方法中添加计时器时,对状态执行单独的请求

- (void)start {

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(updateStatus:) 
                                                 name:kReachabilityChangedNotification 
                                               object:nil];

    Reachability *wifi = [[Reachability reachabilityForLocalWiFi] retain];
    [wifi startNotifier];

    /* Added */
    [NSTimer scheduledTimerWithTimeInterval:0.5F target:self selector:@selector(updateSeparately:) userInfo:nil repeats:YES];
    /* * */

}

以及“updateSeparately”方法本身

/* Added */
- (void)updateSeparately:(NSTimer *)timer {

    NetworkStatus s = [[Reachability reachabilityForLocalWiFi] currentReachabilityStatus];

    if(s == NotReachable) {
        NSLog(@"updateSeparately:Wifi not reachable");
    } else {
        NSLog(@"updateSeparately:Wifi is reachable");
    }
}
/* * */

在控制台中为我提供以下场景的以下输出:

1) AirPort 已打开,我启动应用程序并关闭 AirPort

...
2011-07-21 09:41:41.242 MyProject[7091:207] updateSeparately:Wifi is reachable
2011-07-21 09:41:41.742 MyProject[7091:207] updateSeparately:Wifi is reachable
2011-07-21 09:41:42.242 MyProject[7091:207] updateSeparately:Wifi is reachable
2011-07-21 09:41:42.264 MyProject[7091:207] --- Status Change ---
2011-07-21 09:41:42.265 MyProject[7091:207] Wifi not reachable
2011-07-21 09:41:42.743 MyProject[7091:207] updateSeparately:Wifi not reachable
2011-07-21 09:41:43.243 MyProject[7091:207] updateSeparately:Wifi not reachable
2011-07-21 09:41:43.743 MyProject[7091:207] updateSeparately:Wifi not reachable
...

这似乎是正确的

2) AirPort 关闭后,我再次打开它(应用程序仍在运行)

...
2011-07-21 09:45:42.702 MyProject[7133:207] updateSeparately:Wifi not reachable
2011-07-21 09:45:43.202 MyProject[7133:207] updateSeparately:Wifi not reachable
2011-07-21 09:45:43.701 MyProject[7133:207] updateSeparately:Wifi not reachable
2011-07-21 09:45:43.795 MyProject[7133:207] --- Status Change ---
2011-07-21 09:45:43.795 MyProject[7133:207] Wifi not reachable
2011-07-21 09:45:44.200 MyProject[7133:207] updateSeparately:Wifi not reachable
2011-07-21 09:45:44.700 MyProject[7133:207] updateSeparately:Wifi not reachable
2011-07-21 09:45:45.200 MyProject[7133:207] updateSeparately:Wifi not reachable
2011-07-21 09:45:45.701 MyProject[7133:207] updateSeparately:Wifi is reachable
2011-07-21 09:45:46.201 MyProject[7133:207] updateSeparately:Wifi is reachable
2011-07-21 09:45:46.701 MyProject[7133:207] updateSeparately:Wifi is reachable
...

这表明 NetworkStatus 更改已被注意到......但为什么它保持“NotReachable”大约 2 秒?

有人对此有解释吗?

PS。同样的事情也发生在 Apple 的 Reachability 示例项目中(可在此处获取 http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html)

感谢您的阅读,

米泽尔

最佳答案

在这一点上(9个月后)可能无关紧要,但我正在检查可达性的东西,并找到了从不可到达到可达的 2 秒延迟的可能解释。可能是由于名称解析,如 http://developer.apple.com/library/ios/samplecode/Reachability/Listings/ReadMe_txt.html#//apple_ref/doc/uid/DTS40007324-ReadMe_txt-DontLinkElementID_7 中所述。

IMPORTANT: Reachability must use DNS to resolve the host name before it can determine the Reachability of that host, and this may take time on certain network connections. Because of this, the API will return NotReachable until name resolution has completed. This delay may be visible in the interface on some networks.

我想将检查从命名主机(默认为 apple.com)更改为 IP 应该可以解决问题。

关于iphone - Apple 的 Reachability 源代码中存在严重错误? (本地 WiFi 的可达性),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6772397/

相关文章:

ios - 几次更新后,存档的应用程序不会出现在组织者中

iOS AudioStreamer - 音乐未全长播放

iphone - Apple 目前不接受使用此版本的 sdk(Xcode 4.2、iOS SDK 4.3)构建的应用程序

iphone - 将 Target 从 iPhone- 更新到 Universal-App

iphone - 如何减小 iPhone View 中日期选择器控件的大小

iPhone ivar 命名约定

ios - SpriteKit : add UIGestureRecognizer and detect which node was swiped

iphone - ios 4.3.x 和 5.0 录音后声音中有噪音

iphone - 触摸位置的值超出 View 范围

iphone - CGContextSetRGBFillColor 不工作