SCNetworkReachabilityGetFlags returns 0 even when wireless is available

I have an application that uses Apple's reachability code. When I exit the application, turn on airplane mode, return to the application, I correctly receive a message saying that there is no connection. If I return, turn off the flight mode and return to the application, I ALWAYS receive a message that there is no connection. Specific problem code:

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

I get inside the if statement, and the flags end with 0 ( kSCNetworkReachabilityFlagsTransientConnection ). What exactly does this mean? Has anyone experienced this and does anyone know a workaround or fix? Played with him for hours ...

+4
source share
4 answers

I found that this is caused by providing the host name with a protocol specifier (for example, http://hostname instead of just hostname ). Try specifying only the host name yourself to see if this fixes your problem.

+10
source

After calling SCNetworkReachabilityGetFlags, it is also important to call CFRelease to avoid caching 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; } 
+1
source

If the flags were received, and they are ultimately 0, as you saw, this means that airplane mode is on. However, the results of this check seem to be cached, at least for a short time. Try this: leave your application, turn off airplane mode, click the site in Mobile Safari and return to your application. This may invalidate the cache.

0
source

I had the same problem, but this only happened when I tested in the simulator. I spent 2 days crazy, and then I tested on the device and it worked like a charm! I do not know why...

0
source

Source: https://habr.com/ru/post/1338294/


All Articles