I am working on an iOS app which has a requirement to automatically connect to a WiFi network. We had requested for a NEHotspotHelper extension from Apple which is approved.
Now I am trying to auto connect to the WiFi network around. But the call to method NEHotspotHelper.register never returns. I have added the entitlement com.apple.developer.networking.HotspotHelper in the entitlement file of the app and am also using a newly created provisioning profile with the said entitlement enabled. Trying to get it work from last week. Looks like a small thing which I am finding difficult to catch. Please help.
Below is my code
if let strWiFi = UserDefaults.standard.value(forKey:"WiFiSSID") as? String {
let queue = DispatchQueue(label: "com.mycompany.myapp")
NEHotspotHelper.register(options: nil, queue: queue,
handler: {(_ cmd: NEHotspotHelperCommand) -> Void in
if cmd.commandType == .evaluate || cmd.commandType == .filterScanList {
var hotspot:NEHotspotNetwork?
for network: NEHotspotNetwork in cmd.networkList! {
print("network name:\(network.ssid)")
if (network.ssid == strWiFi) {
network.setConfidence(.high)
let strPassword = UserDefaults.standard.value(forKey:"WiFiPassword") as? String
network.setPassword(strPassword!)
hotspot = network
}
}
let response = cmd.createResponse(.success)
if(hotspot != nil) {
response.setNetwork(hotspot!)
}
response.deliver()
}
})
Any help is highly appreciated!
EDIT: Now when I tried to disconnect and then reconnecting back to the WiFi then it registration succeeds but returns only the details of currently connected network. I need the details of all WiFi networks around.
Is need to enter in Settings -> Wi-fi, the helper only execute if enter in the screen, I tried too make auto connect, but the user need to select the network to connect in the first time. If the user select to connect network once, the next time may auto connect, because may enter in command Maintain.
See more: https://forums.developer.apple.com/message/138756#138756
来源:https://stackoverflow.com/questions/41095687/call-to-nehotspothelper-register-never-returns