SCNetworkReachabilityGetFlags returns 0 even when wireless available

后端 未结 4 1676
花落未央
花落未央 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: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;
    }
    

提交回复
热议问题