当 Wi-Fi 连接但没有互联网时,可达性不起作用 [英] Reachability not working when wi-fi connected but no internet

查看:20
本文介绍了当 Wi-Fi 连接但没有互联网时,可达性不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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:

如何写一个简单的Ping方法在 Cocoa/Objective-C 中

本质上,当您第一次启动应用程序并进行可达性检查时,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆