Using Reachability for Internet *or* local WiFi?
Asked Answered
M

4

11

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.

I was originally only checking for Internet reachability, using:

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

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];

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.

So: what is the appropriate way to determine reachability to either the Internet/WWAN or a local Wifi network (either one, or both)?

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

Monocycle answered 15/4, 2010 at 18:11 Comment(0)
R
21

Use this function to check if wifi is on

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

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

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

(netStatus==ReachableViaWiFi)

if you care that it's over wifi AND

(netStatus==ReachableViaWWAN)

if you care that it's over WWAN

Rothman answered 15/11, 2010 at 16:30 Comment(2)
I think the original question is about getting notified of changes as opposed to testing the current status.Volgograd
notifications didn't work properly for me, but polling repeatedly with the method described above works great.Anechoic
F
7

I ran int the same problem. What I found in the end is that for WWAN or WiFi reachability, the notification callback is only called when there is a network change.

In other words, check the current reachability immediately after you create the reachability object. Do not wait for the notification, because the notification will only be posted when next network change happens.

For Internet/WWAN:

self.wwanReach = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [wwanReach currentReachabilityStatus];
if (networkStatus == ReachableViaWWAN) {
    // do something
}

For WiFi:

self.wifiReach = [Reachability reachabilityForLocalWiFi];
NetworkStatus networkStatus = [wifiReach currentReachabilityStatus];
if (networkStatus == ReachableViaWiFi) {
    // do something
}
Fordone answered 20/2, 2012 at 8:37 Comment(1)
Thanks for the response, this is probably the clearest answer.Surrey
A
1

The reachability example from apple shows you how to use a notification, which is an event push model and supposed to be efficient, but I found that the notifications weren't always working properly for me. Sometimes they wouldn't fire, other times they would provide wrong information. So I ended up polling with a 2 second repeating timer, and checking for wifi with a procedure like the one described by stinkbutt above. Here's a good reference for timer implementation.

Anechoic answered 30/9, 2011 at 2:6 Comment(0)
V
0

I'm not sure if this is your problem, but I noticed that it matters when you call startNotifier. I was having a similar problem with never receiving notifications (but with shorter values for "never" -- I guess I'm not as paitent) until I moved the startNotifier call into some UI code, specifically applicationDidFinishLaunching.

Volgograd answered 8/12, 2010 at 19:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.