iosswifthotspotnetworkextensionnehotspothelper

NEHotspotConfigurationManager getting this alert:"Unable to join the network<name of network>" while error is nil


So i am trying to monitor the connection status by closers :

 func reconnect(success: @escaping () -> Void, failure: @escaping () -> Void) {
    let manager = NEHotspotConfigurationManager.shared
    let ssid = CameraManager.camera.uuid
    let password = "password"
    let isWEP = false
    let hotspotConfiguration = NEHotspotConfiguration(ssid: ssid, passphrase: password, isWEP: isWEP)
    hotspotConfiguration.joinOnce = true
    manager.apply(hotspotConfiguration) { (error) in
        if (error != nil) {

          if let error = error {
                switch error._code {
                case 8:
                    print("internal error")
                    failure()
                case 7:
                    NotificationCenter.default.post(name: Notification.Name(rawValue: "cancelFromHotSpot"), object: nil)
                    failure()
                    self.stopSession()
                case 13:
                    success()
                    DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
                        self.startSession()
                    }
                default:
                    break
                }
        }

        if error == nil {
            print("success connecting wifi")
            success()
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
                self.startSession()
            }
        }
    }
}

Yet there is a scenario that i am getting this alert "Unable to join the network" while error is nil, any ideas?


Solution

  • I think this behavior is a iOS bug and we cannot avoid.

    This problem was also discussed in Apple Developer Forum and answer of Apple staff was below

    "I’ve got nothing to say here beyond what I said on 13 Feb. The fact that errors from the Wi-Fi subsystem don’t get reported via the completion handler is expected behaviour. If you don’t like that behavior — and, to be clear, I personally agree with you about that — the best way forward is to file a bug report requesting that it be changed. Please post your bug number, just for the record."

    This was discussed here

    So I do not have great ideas, unfortunately. All ideas I have are two below (these do not solve this problem perfectly.)

    1. Wait for a bug fix in the future release.
    2. Separate "Applying Configuration" code & Communication code like below.
    @IBAction func setConfigurationButtonTapped(_ sender : Any) {
        manager.apply(hotspotConfiguration) { (error) in
        if(error != nil){
            // Do error handling
        }else{
            // Wait a few seconds for the case of showing "Unable to join the..." dialog.
            // Check reachability to the device because "error == nil" does not means success.
        }
    }
    
    @IBAction func sendButtonTapped(_ sender : Any) {
        self.startSession()
    }