swift--获取Wi-Fi名

 导入所需框架

import SystemConfiguration.CaptiveNetwork
//如果是iOS13及以上需要先获取定位权限 
//首先在info.plist添加key(其中一个即可), NSLocationAlwaysUsageDescription, NSLocationWhenInUseUsageDescription, NSLocationAlwaysAndWhenInUseUsageDescription

if #available(iOS 13.0, *) {
    //系统版本高于13.0 未开启地理位置权限 需要提示一下
    if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.notDetermined {
        self.locationManager = CLLocationManager()
        self.locationManager?.requestWhenInUseAuthorization()
    }
}

if let interfaces = CNCopySupportedInterfaces(), let interfacesArray = CFBridgingRetain(interfaces) as? Array<AnyObject> {
    if interfacesArray.count > 0 {
        let interfaceName = interfacesArray[0] as! CFString
        if let ussafeInterfaceData = CNCopyCurrentNetworkInfo(interfaceName) {
            let interfaceData = ussafeInterfaceData as! Dictionary<String, Any>
            let ssid = interfaceData["SSID"]! as! String
            print(ssid)
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_41735943/article/details/104691829