如何在ios中了解设备连接的网络主机

时间:2016-12-27 05:41:01

标签: ios iphone swift cocoa-touch swift3

如何在ios中了解设备所连接的网络主机。  我想在Swift中学习,我们如何检查我们连接的特定网络。

1 个答案:

答案 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())