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

3

11

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?

Lapides answered 9/12, 2018 at 13:51 Comment(3)
Could you please help with what codes of error exist ?Ternopol
I've also experienced the issue when "Unable to join network" system popup appeared although NEHotspotConfigurationManager.apply(configuration) responded with no error. Resetting networking settings on device helped me ( Settings > General > Transfer or Reset [Device] > Reset > Reset Network). Keep in mind that doing that will forget all your known wifi networks.Melindamelinde
I was facing the same issue. Here is the solution. https://mcmap.net/q/1017239/-nehotspotconfigurationmanager-not-creating-joining-hotspotRadmen
S
10

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()
}
Shoop answered 29/1, 2019 at 13:56 Comment(2)
Yes it is a bug, and i opened one also :)Lapides
Did you get a solution for that ?Gensmer
A
4

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.

Amicable answered 7/10, 2019 at 11:52 Comment(0)
E
0

Remove hotspotConfiguration.joinOnce = true work for me

Englishry answered 28/4, 2022 at 4:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.