Swift3 获取当前连接WIFI名称

1、导入库

import SystemConfiguration
import SystemConfiguration.CaptiveNetwork

 

2、方法

 /// 获取wifi名称
    ///
    /// - Returns: <#return value description#>
    func getUsedSSID() -> String {
        let interfaces = CNCopySupportedInterfaces()
        var ssid = ""
        if interfaces != nil {
            let interfacesArray = CFBridgingRetain(interfaces) as! Array<AnyObject>
            if interfacesArray.count > 0 {
                let interfaceName = interfacesArray[0] as! CFString
                let ussafeInterfaceData = CNCopyCurrentNetworkInfo(interfaceName)
                if (ussafeInterfaceData != nil) {
                    let interfaceData = ussafeInterfaceData as! Dictionary<String, Any>
                    ssid = interfaceData["SSID"]! as! String
                }
            }
        }
        return ssid
    }

 

SSID就是连接wifi的名称,直接使用即可,如果没读到,返回的空""

 

enjoy~

posted @ 2017-04-11 12:46  那一抹风情  阅读(1627)  评论(0编辑  收藏  举报