如何在iOS中使用swift获得可用的wifinetworking名称

我想获得一个地区的所有WiFinetworking和他们的SSID值。 但问题是如何获得所有WiFinetworking的SSID,即使我没有连接到一个。

第一;

导入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 } 

在这里我的class打印WIFInetworking名称

 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 } }