我想获取一个区域内所有可用WiFi网络及其SSID值。但问题在于如何获取所有可用的WiFi网络的SSID,即使我没有连接到其中任意一个。
我想获取一个区域内所有可用WiFi网络及其SSID值。但问题在于如何获取所有可用的WiFi网络的SSID,即使我没有连接到其中任意一个。
iOS 12
请在Xcode中为您的应用启用Access WiFi Information功能,以便在iOS 12及更高版本中使用此功能。启用此功能后,Xcode将自动向您的entitlements文件和App ID添加Access WiFi Information权限。 文档链接
首先:
导入SystemConfiguration.CaptiveNetwork
然后:
func getInterfaces() -> Bool {
guard let unwrappedCFArrayInterfaces = CNCopySupportedInterfaces() else {
print("this must be a simulator, no interfaces found")
return false
}
guard let swiftInterfaces = (unwrappedCFArrayInterfaces as NSArray) as? [String] else {
print("System error: did not come back as array of Strings")
return false
}
for interface in swiftInterfaces {
print("Looking up SSID info for \(interface)") // en0
guard let unwrappedCFDictionaryForInterface = CNCopyCurrentNetworkInfo(interface) else {
print("System error: \(interface) has no information")
return false
}
guard let SSIDDict = (unwrappedCFDictionaryForInterface as NSDictionary) as? [String: AnyObject] else {
print("System error: interface information is not a string-keyed dictionary")
return false
}
for d in SSIDDict.keys {
print("\(d): \(SSIDDict[d]!)")
}
}
return true
}
import UIKit
import Foundation
import SystemConfiguration.CaptiveNetwork
class FirstView: UIViewController
{
@IBOutlet weak var label: UILabel!
override func viewDidLoad()
{
super.viewDidLoad()
let ssid = self.getWiFiName()
print("SSID: \(ssid)")
}
func getWiFiName() -> 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
}
}
var ssid = ""; (CNCopySupportedInterfaces() as? [CFString])?.forEach({ ssid = (CNCopyCurrentNetworkInfo($0) as? [String : Any])?[kCNNetworkInfoKeySSID as String] as? String ?? ""})
- Leo Dabus是的,可以列出所有附近的WiFi网络。您需要在https://developer.apple.com/contact/network-extension上完成问卷调查,然后可以使用NEHotspotHelper返回热点列表。技术问答https://developer.apple.com/library/archive/qa/qa1942/_index.html