- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- 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) updateStatus 消息被触发,当我关闭我的 Mac 的 Airport,然后网络状态为 eq。到“NotReachable”,但是当我再次打开 Mac 的机场时,将触发 updateStatus 消息并且 NETWORKSTATUS 保持“NotReachable”
当我在 start 方法中添加一个计时器时,对状态进行单独请求
- (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 秒?
有人对此有解释吗?
附言。同样的事情发生在 Apple 的可达性示例项目中(可在此处获得 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 源代码中存在严重错误? (reachabilityForLocalWiFi),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6772397/
我想将网络状态通知集成到我的项目中,并为此使用了 Apple 的 Reachability 类。尽管如此,我可能在他们的代码中发现了一个错误,或者它也可能是由模拟器本身引起的。 代码在这里: - (v
我是一名优秀的程序员,十分优秀!