我是斯威夫特的新手。我一直在寻找这个问题很长时间,但仍然找不到解决方案。我了解到它可以访问 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
}
}