我想将网络状态通知集成到我的项目中,并使用Apples的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消息 - 好吧,可能不是bug,也许是正常行为
2)当我关闭Mac的机场然后netstatus是eq时,会触发updateStatus消息。到“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关闭后我再次打开它(App仍在运行)
...
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)
中感谢阅读,
Mizer的
答案 0 :(得分:3)
可能在此时(9个月后)无关紧要,但我正在检查这个可达性的东西,并发现可能的解释是从无法到达的2秒延迟到达。可能是由于名称解析,如http://developer.apple.com/library/ios/samplecode/Reachability/Listings/ReadMe_txt.html#//apple_ref/doc/uid/DTS40007324-ReadMe_txt-DontLinkElementID_7
中所述重要提示:可达性必须使用DNS来解析主机名 它可以确定该主机的可达性,这可能需要一些时间 在某些网络连接上。因此,API将返回 在名称解析完成之前无法访问。这种延迟可能是 在某些网络的界面中可见。
我想将检查从命名主机(默认情况下为apple.com)更改为ip应该可以解决问题。
答案 1 :(得分:2)
使用此可访问性类here
我在所有应用中都使用它,效果非常好。