1

我正在尝试使用NEHotspotConfigurationManager没有任何运气连接到开放的 Wi-Fi 网络。我已确保我的应用程序具有正确的热点配置权利,并且我在 > iOS 11 的设备上运行。

这是我用来连接到开放网络的代码。

// MARK: - Connect to Hotspot
@available(iOS 11.0, *)
func connectToHotspot(completion: @escaping APConnectionStatusHandler) {
    let configuration = NEHotspotConfiguration.init(ssid: Constants.hotspotSSID)
    configuration.joinOnce = true

    NEHotspotConfigurationManager.shared.apply(NEHotspotConfiguration.init()) { connectionError in
        if let error = connectionError {
            debugPrint("Failed to automatically connect to \(Constants.hotspotSSID)")
            debugPrint(error)
            completion(false, error.localizedDescription)
        }
        else {
            debugPrint("Automatically connected to \(Constants.hotspotSSID)")
            completion(true, nil)
        }
    }
}

connectionError每次我运行它时都会填充:

Domain=NEHotspotConfigurationErrorDomain Code=1 "invalid SSID." 

我无法找到有关此错误消息的确切含义的任何信息。网络显示在我正在使用的设备的网络列表中。我拼写正确,无论我使用什么 SSID 字符串,错误消息都是相同的。

有什么建议吗?

4

1 回答 1

0

也许你不应该使用 Constant.hotspotSSID。

根据上述,我无法确定什么是“常量”或它是哪种数据类型。

这是我的想法,试试这个:

            let yourSSID: String = "SSID"
            let configuration = NEHotspotConfiguration.init(ssid: yourSSID)
            configuration.joinOnce = true
            NEHotspotConfigurationManager.shared.apply(configuration) {
                (error) in
                if error != nil {
                    print("Connect-> Failure!")
                } else {
                    print("Connect-> Success!")
                }
            }
于 2020-03-16T09:01:13.687 回答