scan available wifi with HotspotHelper

362 Views Asked by At

I'm new to Swift. I've been looking for this question for a long time but still cannot find a solution. I'd learned that it is able to access wifi for iOS 12, but I also saw some people said that It is not possible to get all the available wifi networks names and information...

Now it's able to get the connected wifi name in my code, but what I want is to also list all the available wifi name nearby, like the Setting page in iPhone. I've already abled the capabilities of "Access Wif Information", "Hotspot Configuration", "Network Extension", "Personal VPN" and "Wireless Accessory Configuration ". But I found that the NEHotspotHelper.register() return false all the time. Can anyone tell me why that happens or how to fix that? or is it able to jump to the Setting page of the iPhone in the app? thanks a lot. Also, I'm not a native English speaker, my English is quite poor, thanks for reading my question.

Here's my code:

import UIKit
import NetworkExtension
import SystemConfiguration
import SystemConfiguration.CaptiveNetwork

class SearchVC: UIViewController {
    @IBAction func SearchAction(_ sender: UIButton) {
        print(fetchSSIDInfo())
        let targetSsid = fetchSSIDInfo()
        let targetAnnotation: String = targetSsid!

    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
        }
    }
    if isAvailable{
        print("isAvailable")
    }else{print("notAvailable")}
}

func fetchSSIDInfo() -> String? {
    var ssid: String?
    if let interfaces = CNCopySupportedInterfaces() as NSArray? {
        for interface in interfaces {
            if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
                ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
                break
            }
        }
    }
    return ssid
}
}
0

There are 0 best solutions below