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...

jjxtra
  • 20,415
  • 16
  • 100
  • 140

4 Answers4

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.

jhabbott
  • 18,461
  • 9
  • 58
  • 95
  • 1
    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! – idStar May 23 '12 at 15:17
  • We are using only the host name and always have been. I believe the real problem was some really bad corporate WiFi. – jjxtra Apr 25 '13 at 20:56
  • 1
    Since this solved the problem for some people, I'll accept this answer. – jjxtra Aug 05 '14 at 16:21
  • Thanks a lot! As I noticed it should be "root" hostname without subdirectories. – Tzoiker Jul 27 '15 at 11:15
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;
}
Bissy
  • 131
  • 3
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...

Adriana
  • 225
  • 3
  • 8
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.

Don
  • 3,654
  • 1
  • 26
  • 47