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.
Post
Replies
Boosts
Views
Activity
Date/Time: 2023-12-22 10:15:42.0985 +0800
Launch Time: 2023-12-22 10:12:52.8757 +0800
OS Version: iPhone OS 17.1.2 (21B101)
Release Type: User
Baseband Version: 4.06.02
Report Version: 104
Exception Type: EXC_CRASH (SIGKILL)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Termination Reason: FRONTBOARD 3131951633
<RBSTerminateContext| domain:10 code:0xBAADCA11 explanation: reportType:CrashLog maxTerminationResistance:Interactive>
Triggered by Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libsystem_kernel.dylib 0x1e6bfc1d8 mach_msg2_trap + 8
1 libsystem_kernel.dylib 0x1e6bfbf70 mach_msg2_internal + 79
2 libsystem_kernel.dylib 0x1e6bfbe88 mach_msg_overwrite + 435
3 libsystem_kernel.dylib 0x1e6bfbcc8 mach_msg + 23
4 CoreFoundation 0x19f725d0c __CFRunLoopServiceMachPort + 159
5 CoreFoundation 0x19f723c04 __CFRunLoopRun + 1207
6 CoreFoundation 0x19f723668 CFRunLoopRunSpecific + 607
7 GraphicsServices 0x1e2aee5ec GSEventRunModal + 163
8 UIKitCore 0x1a1b402b4 -[UIApplication _run] + 887
9 UIKitCore 0x1a1b3f8f0 UIApplicationMain + 339
10 iCSee 0x103921380 main + 115 (main.m:17)
11 dyld 0x1c2176dcc start + 2239