Swift3 获取当前连接WIFI名称

时间:2021-03-15 15:16:12

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 > {
let interfaceName = interfacesArray[] 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~