如何在ios中了解设备所连接的网络主机。 我想在Swift中学习,我们如何检查我们连接的特定网络。
答案 0 :(得分:1)
使用此自定义类访问您当前连接到的wifi网络: -
import Foundation
import SystemConfiguration.CaptiveNetwork
public class SSID {
class func fetchSSIDInfo() -> String {
var currentSSID = ""
if let interfaces = CNCopySupportedInterfaces() {
for i in 0..<CFArrayGetCount(interfaces) {
let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interfaces, i)
let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString)
if unsafeInterfaceData != nil {
let interfaceData = unsafeInterfaceData! as NSDictionary!
print(interfaceData)
currentSSID = interfaceData?.value(forKey:"SSID") as! String
}
}
}
return currentSSID
}
}
您可以通过这种方式获取信息: -
print(SSID.fetchSSIDInfo())