我想获取一个地区所有可用的 WiFi 网络及其 SSID 值。但问题是如何获得所有可用 WiFi 网络的 SSID,即使我没有连接到一个网络。
最佳答案
iOS 12
您必须启用从功能中访问 WiFi 信息。
Important To use this function in iOS 12 and later, enable the Access WiFi Information capability for your app in Xcode. When you enable this capability, Xcode automatically adds the Access WiFi Information entitlement to your entitlements file and App ID. Documentation link
首先;
导入 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 }
关于ios - 如何使用 swift 在 iOS 中获取可用的 wifi 网络名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31715055/