iPhone Wifi on or off?

后端 未结 6 1424
失恋的感觉
失恋的感觉 2020-12-03 16:27

Within iOS framework, how can one check if the Wifi radio is enabled by the user or not? Please note that I\'m not interested in the reachability through Wifi but rather if

6条回答
  •  情话喂你
    2020-12-03 17:08

    iOS has no public API that tells you if Wi-Fi is on or off.

    However, you can use the public API CNCopyCurrentNetworkInfo(), available in SystemConfiguration framework, to get info about the current Wi-Fi network. This does not tell you if Wi-Fi is turned on or off but rather if the device is joined to a Wi-Fi network or not. Perhaps this is sufficient for your purposes.

    Also note that this API doesn't work in Simulator, as CNCopySupportedInterfaces() always returns NULL.

    #include 
    
    BOOL hasWiFiNetwork = NO;
    NSArray *interfaces = CFBridgingRelease(CNCopySupportedInterfaces());
    for (NSString *interface in interfaces) {
        NSDictionary *networkInfo = CFBridgingRelease(CNCopyCurrentNetworkInfo((__bridge CFStringRef)(interface)));
        if (networkInfo != NULL) {
            hasWiFiNetwork = YES;
            break;
        }
    }
    

提交回复
热议问题