一般我们获取wifi信息都是通过SystemConfiguration.framework里的CaptiveNetwork 。但是升级完xcode 10 运行以前的工程发现获取不到了。不要慌,不是说方法用不了了,这是因为xcode10里添加了一个Capabilitie。需要你手动打开。如下图:
Access WiFi Infomation
打开这个你就会发现可以获取到了。
下面丢一个获取方法:
func getWiFiNetworks() -> [String] {
var wifiNetworks = [String]()
guard let unwrappedCFArrayInterfaces = CNCopySupportedInterfaces() else {
print("this must be a simulator, no interfaces found")
return wifiNetworks
}
guard let swiftInterfaces = (unwrappedCFArrayInterfaces as NSArray) as? [String] else {
print("System error: did not come back as array of Strings")
return wifiNetworks
}
for interface in swiftInterfaces {
guard let unwrappedCFDictionaryForInterface = CNCopyCurrentNetworkInfo(interface as CFString) else {
print("System error: \(interface) has no information")
return wifiNetworks
}
guard let SSIDDict = (unwrappedCFDictionaryForInterface as NSDictionary) as? [String: AnyObject] else {
print("System error: interface information is not a string-keyed dictionary")
return wifiNetworks
}
for d in SSIDDict.keys {
wifiNetworks.append(String(describing: SSIDDict[d]!))
}
}
return wifiNetworks
}
网友评论