SCNetworkReachabilityGetFlags returns 0 even when wireless available
Asked Answered
R

4

5

I have an app that uses Apples reachability code. When I tab out of the app, turn on airplane mode, go back into the app, I correctly get a message that says no connection is available. If I go back out turn OFF airplane mode and go back into the app, I STILL get the message that no connection is available. The specific problem code is this:

NetworkStatus status = kNotReachable;
if (SCNetworkReachabilityGetFlags(reachabilityRef, &flags))
{
    status = [self networkStatusForFlags: flags];
    return status;
}

I get inside the if statement and flags ends up being 0 (kSCNetworkReachabilityFlagsTransientConnection). What does that mean exactly? Has anyone experienced this and does anyone know a work-around or fix? Been playing with it for hours...

Romola answered 3/2, 2011 at 17:30 Comment(5)
kSCNetworkReachabilityFlagsTransientConnection has the value 1<<0 (which is 1). You are getting 0, which means none of the flags are set.Wersh
@PsychoDad how did u resolve the issue?Coastwise
@Coastwise I think the problem was with some really bad corporate WiFi. I haven't seen the problem in over a year now.Romola
@PsychoDad Can u please paste the code or explain what you did different than the code mentioned aboveCoastwise
Code has not changed and is the same as what is in the questionRomola
W
11

I have found that this is caused by supplying a hostname with a protocol specifier (such as http://hostname instead of just hostname). Try specifying just the hostname by itself to see if this fixes your problem.

Wersh answered 6/1, 2012 at 18:10 Comment(4)
This solved the issue for me. I was checking with Reachability for a website to load in a UIWebView. By using the website domain for Reachability purposes, but then using the same domain prepended by protocol for UIWebView loading purposes, all worked fine again. Why it worked prior, I'm not sure. Seems to have become more finicky for me after the iOS5.1.1 update. Good advice @jhabbott!Brit
We are using only the host name and always have been. I believe the real problem was some really bad corporate WiFi.Romola
Since this solved the problem for some people, I'll accept this answer.Romola
Thanks a lot! As I noticed it should be "root" hostname without subdirectories.Apia
B
3

After you call SCNetworkReachabilityGetFlags it is important also to call CFRelease to avoid caching the network status. See my implementation below:

SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name);

SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags);
bool isAvailable = success && (flags & kSCNetworkFlagsReachable) &&
!(flags & kSCNetworkFlagsConnectionRequired);
CFRelease(reachability);
if (isAvailable) {
    NSLog(@"Host is reachable: %d", flags);
    return true;
}else{
    NSLog(@"Host is unreachable");
    return false;
}
Burkey answered 17/1, 2013 at 14:24 Comment(1)
This answer solved my problem. Apple really should mention this in their documents.Veneaux
I
0

If the flags were received and they end up being 0, as you've seen, this indicates Airplane Mode is on. However, the results of this check seem to be cached, at least for a short time. Try this: leave your app, turn Airplane Mode off, hit a site in Mobile Safari, then return to your app. This may invalidate the cache.

Inverness answered 16/5, 2011 at 19:16 Comment(0)
P
0

I had the same problem but it was only happening when i was testing in the simulator. I spent 2 days going crazy and then I tested on the device and it worked as a charm! No idea why...

Phocine answered 1/8, 2017 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.