NEHotspotConfigurationManager 收到此警报:"Unable to join the network<name of network>" 而错误为零

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

所以我正在尝试通过 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()
            }
        }
    }
}

但有一种情况是我收到此警报 "Unable to join the network" 而错误为零,有什么想法吗?

我认为此行为是一个 iOS 错误,我们无法避免。

Apple Developer Forum 也讨论了这个问题,Apple 工作人员的回答如下

"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

不幸的是,我没有什么好主意。我所有的想法是下面两个(这些都不能完美解决这个问题。)

  1. 等待未来版本中的错误修复。
  2. 分开 "Applying Configuration" 代码和通信代码,如下所示。
@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()
}

iOS 13 - 补丁

我遇到了同样的问题,但我通过先删除所有现有的配置条目解决了这个问题:

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

也许它并不总是一个可行的解决方案,因为它有点激烈,但对我来说就像一个魅力。

PS:我在一个运行 iOS 13 的应用程序中使用它。据我所知,应该也适用于 iOS 11 和 12,但是我没测试。

删除 hotspotConfiguration.joinOnce = true 为我工作