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)
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章