SCNetworkReachabilityGetFlags returns 0 even when wireless available

后端 未结 4 1666
花落未央
花落未央 2020-12-17 03:21

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 i

相关标签:
4条回答
  • 2020-12-17 03:41

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

    0 讨论(0)
  • 2020-12-17 03:53

    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;
    }
    
    0 讨论(0)
  • 2020-12-17 03:56

    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.

    0 讨论(0)
  • 2020-12-17 03:56

    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.

    0 讨论(0)
提交回复
热议问题