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

筅森魡賤 提交于 2019-12-23 09:29:32

问题


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?


回答1:


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()
}



回答2:


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.



来源:https://stackoverflow.com/questions/53693025/nehotspotconfigurationmanager-getting-this-alertunable-to-join-the-networknam

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!