使用Internet *或*本地WiFi的可达性? [英] Using Reachability for Internet *or* local WiFi?

查看:173
本文介绍了使用Internet *或*本地WiFi的可达性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了SO以寻找这个问题的答案,它并没有真正解决,至少不能达到我可以使其工作的程度。

I've searched SO for the answer to this question, and it's not really addressed, at least not to a point where I can make it work.

我最初只使用以下方法检查Internet可达性:

I was originally only checking for Internet reachability, using:

self.wwanReach = [Reachability reachabilityWithHostName:@"www.apple.com"];
[wwanReach startNotifer];

我现在需要支持一个本地WiFi连接当我发现+ reachabilityForLocalWiFi,我也注意到有+ reachabilityForInternetConnection。我想我可以使用这些,而不是硬编码www.apple.com在那里,但唉,当我使用

I now need to support a local WiFi connection (in the absence of reaching the Internet in general), and when I found +reachabilityForLocalWiFi, I also noticed there was +reachabilityForInternetConnection. I figured I could use these, instead of hard-coding "www.apple.com" in there, but alas, when I use

self.wwanReach = [Reachability reachabilityForInternetConnection];
[wwanReach startNotifer];
self.wifiReach = [Reachability reachabilityForLocalWiFi];
[wifiReach startNotifer];

我设置的从不的可达性回调被调用,最多10,12,15分钟左右(这是只要我的耐心持续了(用户的耐心会少很多,我敢肯定)。切换回+ reachabilityWithHostName:工作在几秒钟内,我还尝试每个对

the reachability callback that I've set up "never" gets called, for values of "never" up to 10, 12, 15 minutes or so (which was as long as my patience lasted. (User's patience will be much less, I'm sure.) Switching back to +reachabilityWithHostName: works within seconds. I also tried each "pair" individually, in case there was an issue with two notifiers in progress simultaneously, but that made no difference.

因此:确定可达性的适当方法是什么?

[这个特定的使用案例是iPhone或iPad连接到互联网/ WWAN或本地Wifi网络]

[This particular use case is an iPhone or iPad connecting to a Mac mini computer-to-computer network; I'm sure other situations apply.]

推荐答案

使用此函数检查是否WiFi已开启

Use this function to check if wifi is on

- (BOOL)isWifiOn {
    Reachability* wifiReach = [Reachability reachabilityForLocalWiFi];

    NetworkStatus netStatus = [wifiReach currentReachabilityStatus];
    return (netStatus==ReachableViaWiFi);
}


$ b < >

similar code can be used to check reachabilityForInternetConnection but you have to check

(netStatus==ReachableViaWiFi)


(netStatus==ReachableViaWWAN)

如果你关心它在WWAN

if you care that it's over WWAN

这篇关于使用Internet *或*本地WiFi的可达性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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