【问题标题】:How to know in ios that with which network host the device is connected如何在 ios 中知道设备与哪个网络主机连接
【发布时间】:2016-12-27 05:41:00
【问题描述】:
如何在 ios 中知道设备连接到哪个网络主机。
我想在 Swift 中学习,我们如何检查我们连接的特定网络。
【问题讨论】:
标签:
ios
iphone
swift
cocoa-touch
swift3
【解决方案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())