iOS 11.0 - Periodically scan for SSIDs (WiFi) nearby

被刻印的时光 ゝ 提交于 2019-11-29 04:01:37

Unfortunately, we can't achieve this in iOS.

When I learned about NEHotspotConfiguration, I was initially excited, because I was hoping we could automate this process. However, it doesn't look like there's a way to retrieve a list of currently available SSIDs, which makes NEHotspotConfiguration much less useful for our use case.

陈健 Mark

Not possible.

I enabled the NEHotspotHelper permission and tried on a Demo app. Even though Apple does allow you to configure wifi connections, in order to get the list of available networks, you need to open the Settings > WiFi page to get the list. This API can NOT offer the ability to scan the WiFi list in background, not to mention periodically.

It's a pity for developers, but I think it's good for iOS users, to protect privacy and safety by strict restriction of the API usage.

As per my understanding you can achieve this using following code

      let targetSsid = getCurrentSSID()
    let targetAnnotation: String = targetSsid!

    //let options: [String: NSObject] = [kNEHotspotHelperOptionDisplayName : targetAnnotation as NSObject]
    //let queue: DispatchQueue = DispatchQueue(label: "com.myapp.appname", attributes: DispatchQueue.Attributes.concurrent)

    NSLog("Started wifi list scanning.")


    let options: [String: NSObject] = [
        kNEHotspotHelperOptionDisplayName: targetAnnotation as NSString
    ]

    let queue = DispatchQueue(label: "com.example.test")

    let isAvailable = NEHotspotHelper.register(options: options, queue: queue) { (command) in
        switch command.commandType {
        case .evaluate,
             .filterScanList:
            let originalNetworklist = command.networkList ?? []
            let networkList = originalNetworklist.compactMap { network -> NEHotspotNetwork? in
                print("networkName: \(network.ssid); strength: \(network.signalStrength)")
                if network.ssid == targetSsid {
                    network.setConfidence(.high)
                    //network.setPassword(targetPassword)
                    return network
                }
                return nil
            }
            let response = command.createResponse(.success)
            response.setNetworkList(networkList)
            response.deliver()
        default:
            break
        }
    }

Only problem is that you have to be at wifi settings page to get the list of all available SSIDs along with their signal strength.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!