当Wi-Fi连接但没有互联网时,可达性不起作用 [英] Reachability not working when wi-fi connected but no internet
问题描述
我正在使用Apple的可访问性代码,并在设置网络可访问性更改时以及启动服务器连接之前设置两个初始通知。当我在wi-fi上时,代码工作,我关闭了Wi-Fi接入点。但是,当我使用wi-fi启动应用程序并使用底层宽带连接时,然后一旦应用程序运行,然后断开wi-fi路由器与宽带路由器的连接(即Wi-Fi已打开,但没有互联网连接),我做了一个可达性检查,我得到的网络状态是ReachableViaWiFi。我已经尝试了reachabilityForInternetConnection和reachabilityWithHostName。
I am using Apple's reachability code, and am setting up both initial notifications on when network reachability changes and prior to initiating a server connection. The code works when I am on wi-fi and I turn the wi-fi access point off. However, when I start the app with wi-fi and the underlying broadband connection working, and then once the app is running, and then disconnect the wi-fi router from the broadband router (i.e. Wi-Fi is on but there is no internet connectivity), and I do a reachability check, the network status I get is ReachableViaWiFi. I have tried both reachabilityForInternetConnection and reachabilityWithHostName.
关于Apple的可达性代码是否可用于检测wifi已连接但没有底层网络连接的任何想法?
Any ideas on if Apple's reachability code can be used to detect a situation where the wifi is connected but there is no underlying network connectivity?
谢谢!
推荐答案
好的,我找到了答案这 - Apple的可达性不测试与主机的实际连接。请在下面的SO链接中查看@Zhami的答案:
ok, I found out the answer to this - Apple's reachability does not test actual connectivity to the host. See the answer by @Zhami in the SO link below:
如何在Cocoa / Objective-C中编写简单的Ping方法
基本上,当您第一次启动应用程序并进行可访问性检查时,iOS似乎进行DNS查找,如果没有互联网,则检查失败。因此,第一次检查可达性时,它实际上返回一个有意义的值。但是,如果您在应用程序启动时连接,并在一段时间后丢失互联网连接(虽然仍然连接到WiFi / 3G / 4G但没有底层互联网连接),即使互联网或您指定的主机无法访问,仍可以进一步访问可达性检查再过。
Essentially, when you first launch the app and do a reachability check, iOS seems to do a DNS lookup, and if there is no internet, the check fails. So the first time you check reachability , it actually returns a meaningful value. However, if you are conected at app launch, and lose internet connectivity after some time (while still connected to WiFi/3G/4G but no underlying internet connectivity), further reachability checks return reachable even though the internet or your specified host is not reachable anymore.
因此,如果您想真正实时检查连接,请考虑使用以下内容:
So if you want to really check for connectivity in real time, consider using the following:
-(BOOL) isConnected
{
NSString* url = [NSURL URLWithString:@"http://www.google.com/m"];
ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
[request setTimeOutSeconds:10];
//customize as per your needs - note this check is synchronous so you dont want to block the main thread for too long
[request setNumberOfTimesToRetryOnTimeout:0];
[request startSynchronous];
NSError *error = [request error];
if (error)
{
DLog(@"connectivity error");
return NO;
}
else
{
DLog(@"connectivity OK");
return YES;
}
}
这篇关于当Wi-Fi连接但没有互联网时,可达性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!