Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SCNetworkReachabilityGetFlags returns 0 even when wireless available

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

like image 381
jjxtra Avatar asked Feb 03 '11 17:02

jjxtra


2 Answers

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.

like image 63
jhabbott Avatar answered Sep 18 '22 17:09

jhabbott


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;
}
like image 25
Bissy Avatar answered Sep 22 '22 17:09

Bissy