所以我正尝试通过闭合器监视连接状态:
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()
}
}
}
}
但是,在某些情况下,我得到此警报“无法加入网络”,而错误为零,有什么主意吗?
答案 0 :(得分:1)
我认为这种行为是iOS的错误,我们无法避免。
在苹果开发者论坛上也讨论了这个问题,下面是苹果员工的回答
“除了2月13日我所说的以外,我无话可说。这是Wi-Fi子系统的错误不会通过完成处理程序报告的事实,这是预期的行为。如果您不喜欢那样,行为-明确地说,我个人对此表示同意-最好的解决方法是提交一个错误报告,要求对其进行更改。请张贴您的错误号,以供记录。“
不幸的是,所以我没有好主意。我的所有想法都在下面两个(不能完全解决这个问题。)
@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()
}
答案 1 :(得分:0)
iOS 13-补丁
我遇到了同样的问题,但是我先删除了所有现有的配置条目来解决了这个问题:
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
}
也许有些困难,但这并不总是可行的解决方案,但对我而言,它就像一个魅力。
PS:我在运行iOS 13的应用程序中使用了它。据我所知,它也可以在iOS 11和12上运行,但是我没有对其进行测试。