I am developing an iOS application based on Objective-C, and I have encountered a requirement where I need to determine if the password for the currently connected Wi-Fi is empty. If it is empty, the user is allowed to proceed to the next step. If it is not empty, they must enter a password. This will be used in the next process, which is the network configuration of a physical device.
After researching documentation, I found two possible approaches to determine if the Wi-Fi password is empty. The first approach is to directly check the encryption type of the current Wi-Fi. If there is no encryption type, the Wi-Fi password is empty. The second approach is to use Apple's NEHotspotConfiguration class to attempt connecting to the Wi-Fi and determine if the password is empty. However, both approaches have encountered issues.
For the first approach, there seems to be no public API available to directly retrieve the Wi-Fi encryption type.
For the second approach, when using NEHotspotConfiguration to connect, I first get the Wi-Fi's SSID and then attempt to connect with an empty password. I am using [NEHotspotConfiguration alloc] initWithSSID:ssid] to create a configuration, and then I call [NEHotspotConfigurationManager sharedManager] applyConfiguration: to connect. However, regardless of whether the Wi-Fi is actually encrypted or unencrypted, no error is returned. The code is as follows:
NSString *ssid = [NetInterface getCurrent_SSID]; // The Wi-Fi SSID that needs to be checked
NEHotspotConfiguration *configuration = [[NEHotspotConfiguration alloc] initWithSSID:ssid];
configuration.joinOnce = YES;
// Remove previous configuration (optional)
[[NEHotspotConfigurationManager sharedManager] removeConfigurationForSSID:ssid];
self.isWiFiEmptyOperateState = 1;
// Attempt to apply the new configuration
[[NEHotspotConfigurationManager sharedManager] applyConfiguration:configuration completionHandler:^(NSError * _Nullable error) {
self.isWiFiEmptyOperateState = 2;
if (error) {
if (error.code == NEHotspotConfigurationErrorInvalid) {
NSLog(@"Wi-Fi %@ is encrypted, a password is required", ssid);
} else if (error.code == NEHotspotConfigurationErrorUserDenied) {
NSLog(@"User denied the Wi-Fi configuration");
} else {
NSLog(@"Other error: %@", error.localizedDescription);
}
} else {
NSLog(@"Successfully connected to Wi-Fi %@, this network might be open", ssid);
}
}];
In the code above, it always ends up logging "Successfully connected to Wi-Fi." Is there any other approach that can fulfill my functional requirement? I noticed that some apps on the App Store have implemented this functionality, but all my attempts so far have failed.
there seems to be no public API available to directly retrieve the Wi-Fi encryption type.
Yeah there is. Follow the process described in the Current Wi-Fi network section of TN3111 iOS Wi-Fi API overview and then look at the securityType
property.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"