NEHotspotConfigurationManager对于无效密码/ SSID没有错误

Kyl*_*arn 5 wifi ios swift networkextension nehotspothelper

当我使用NEHotspotConfigurationManager连接到WiFi接入点,并且我故意使用无效密码或SSID时,我没有得到我期望的程序化反馈.用户通过警报接收连接失败的反馈,但在提供给该apply功能的完成块中,error为零,与成功案例相同.这使我无法区分成功和失败案例.在NEHotspotConfigurationError既有.invalidSSID.invalidWPAPassphrase.我希望这些会被退回.这对我来说就像一个雷达,但我想先在这里得到一些反馈.

NEHotspotConfigurationManager.shared.removeConfiguration(forSSID: "test")
let configuration = NEHotspotConfiguration(ssid: "test", passphrase: "testasdasd", isWEP: false)
configuration.joinOnce = true
NEHotspotConfigurationManager.shared.apply(configuration) { (error) in
    // error is nil
}
Run Code Online (Sandbox Code Playgroud)

Kyl*_*arn 11

在向Apple提交雷达后,看来这个API正在按设计运行.成功/失败情况仅适用于热点配置的应用.

好消息是,我已经找到了一个合适的工作,用于CNCopySupportedInterfaces验证应用程序是否确实连接到它所说的SSID.

let ssid = "test"
NEHotspotConfigurationManager.shared.removeConfiguration(forSSID: said)
let configuration = NEHotspotConfiguration(ssid: ssid, passphrase: "testasdasd", isWEP: false)
configuration.joinOnce = true
NEHotspotConfigurationManager.shared.apply(configuration) { (error) in
    if let error = error as NSError? {
            // failure
        } else {
            if self.currentSSIDs().first == ssid {
                // Real success
            } else {
                // Failure
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

使用以下定义的此功能:

func currentSSIDs() -> [String] {
    guard let interfaceNames = CNCopySupportedInterfaces() as? [String] else {
        return []
    }
    return interfaceNames.flatMap { name in
        guard let info = CNCopyCurrentNetworkInfo(name as CFString) as? [String:AnyObject] else {
            return nil
        }
        guard let ssid = info[kCNNetworkInfoKeySSID as String] as? String else {
            return nil
        }
        return ssid
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这是个很好的信息,对于XCode 10,请确保您设置了AccessWiFi权限,而不是只有您才能访问SSID名称。 (2认同)
  • 在链接库中添加SystemConfiguration框架并“导入SystemConfiguration.CaptiveNetwork” (2认同)