Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 692
ironRoei Avatar asked Dec 09 '18 13:12

ironRoei


2 Answers

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()
}
like image 107
MORI Avatar answered Oct 20 '22 03:10

MORI


iOS 13 - Patch

I had the same problem, but I solved it by deleting first all the existing configuration entries:

NEHotspotConfigurationManager.shared.getConfiguredSSIDs { (wifiList) in
    wifiList.forEach { NEHotspotConfigurationManager.shared.removeConfiguration(forSSID: $0) }
    // ... from here you can use your usual approach to autoconnect to your network
}

Maybe it's not always a possible solution since it's a bit drastic, but for me worked like a charm.

PS: I use this in an app that runs iOS 13. As far as I know should work also on iOS 11 and 12, but I didn't test it.

like image 3
Alessandro Francucci Avatar answered Oct 20 '22 03:10

Alessandro Francucci