使用HotspotHelper扫描可用的wifi

时间:2019-03-15 04:02:30

标签: swift4 wifi xcode10.1 nehotspothelper

我是Swift的新手。我一直在寻找这个问题很长时间,但仍然找不到解决方案。我了解到它可以访问iOS 12的wifi,但是我也看到有人说不可能获得所有可用的wifi网络名称和信息...

现在它可以在我的代码中获取连接的wifi名称,但是我想在附近列出所有可用的wifi名称,例如iPhone中的“设置”页面。我已经具备了“访问Wif信息”,“热点配置”,“网络扩展”,“个人VPN”和“无线附件配置”的功能。但是我发现NEHotspotHelper.register()一直都返回false。谁能告诉我为什么会这样或如何解决?还是可以跳至应用程序中iPhone的“设置”页面?非常感谢。另外,我不是英语为母语的人,我的英语很差,谢谢您阅读我的问题。

这是我的代码:

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 个答案:

没有答案