Merry
Merry

Reputation: 460

Wi-Fi Programmatic Connection Issue: Correct Password Fails After Initial Incorrect Attempt in iOS

I am trying to programmatically connect to a Wi-Fi network in my iOS application using NEHotspotConfiguration. Here's the issue I am encountering:

When I try to connect with an incorrect password for the first time, it shows an error: "Failed to connect: Not associated with the expected SSID." (which is expected behavior). After that, I correct the password and try to connect again. However, it still shows the same error ("Failed to connect: Not associated with the expected SSID.") and does not connect to the network, even though the password is correct.

Here is the code I am using:

  func connectToWiFi(ssid: String, password: String, joinOnce: Bool, completion: @escaping (Bool, String) -> Void) {
       
        // First, remove any existing configuration for the SSID to avoid caching issues
        NEHotspotConfigurationManager.shared.removeConfiguration(forSSID: ssid)

        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            
            let hotspotConfiguration = NEHotspotConfiguration(ssid: ssid, passphrase: password, isWEP: false)
            hotspotConfiguration.joinOnce = joinOnce
            
            NEHotspotConfigurationManager.shared.apply(hotspotConfiguration) { error in
                if let error = error {
                    if (error as NSError).code == NEHotspotConfigurationError.alreadyAssociated.rawValue {
                        print("Already connected to the Wi-Fi: \(ssid)")
                        completion(true,"Already connected to the Wi-Fi: \(ssid)")
                    } else {
                        print("Failed to connect to Wi-Fi: \(ssid), Error: \(error.localizedDescription)")
                        completion(false, "Failed to connect to Wi-Fi: \(ssid), Error: \(error.localizedDescription)")
                    }
                } else {
                    print(WiFiSSIDManager.shared.currentSSIDs())
                    if WiFiSSIDManager.shared.currentSSIDs().first == ssid {
                        // Real success
                        completion(true, "Successfully connected to Wi-Fi: \(ssid)")
                    } else {
                        // Failure
                        completion(false, "Failed to connect: Not associated with the expected SSID.")
                    }
                }
            }
        }
    }

  func currentSSIDs() -> [String] {
        var ssid: [String] = []
                if let interfaces = CNCopySupportedInterfaces() as NSArray? {
                    for interface in interfaces {
                        if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
                            if let currentSSID = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String {
                                ssid.append(currentSSID)
                            }
                        }
                    }
                }
                return ssid
    }
    

Is there a way to fully reset the Wi-Fi state for a specific SSID programmatically in iOS so that the connection attempt with the correct password works after a previous incorrect attempt?

Are there any additional steps needed to ensure that the device properly clears cached credentials or network associations?

Any help or guidance would be greatly appreciated. Thank you!

Upvotes: 0

Views: 26

Answers (0)

Related Questions