【问题标题】:How to know in ios that with which network host the device is connected如何在 ios 中知道设备与哪个网络主机连接
【发布时间】:2016-12-27 05:41:00
【问题描述】:

如何在 ios 中知道设备连接到哪个网络主机。 我想在 Swift 中学习,我们如何检查我们连接的特定网络。

【问题讨论】:

  • 使用可达性框架,它会帮助你解决问题
  • Reachability 来自苹果
  • AFNetworking 也有帮助,包括可达性

标签: 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())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    相关资源
    最近更新 更多