iOS 获取系统当前的Wi-Fi名称为空的检查项和iOS 14后方法替换

 

Starting with iOS 14, the SSID and BSSID of the currently connected Wi-Fi network can be obtained using the fetchCurrent(completionHandler:) method of the NEHotspotNetwork class. Note that the use of fetchCurrent(completionHandler:) does not require the NEHotspotHelper entitlement.

 

This method returns SSID and BSSID of the current Wi-Fi network when the requesting application meets one of following 4 requirements:

  1. application is using CoreLocation API and has user's authorization to access precise location.
  2. application has used NEHotspotConfiguration API to configure the current Wi-Fi network.
  3. application has active VPN configurations installed.
  4. application has active NEDNSSettingsManager configuration installed.

An application will receive nil if it fails to meet any of the above 4 requirements.

An application will receive nil if does not have the "com.apple.developer.networking.wifi-info" capability.

 

iOS 14以前获取方式:

public func getSSID() -> String? {
  var ssid: String?
   if let interfaces = CNCopySupportedInterfaces() as NSArray? {
    for interface in interfaces {
      if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {
        ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
        break
          }
       }
    }
    return ssid
}

 iOS 14以后获取方式:

func getNetworkInfo(compleationHandler: @escaping ([String: Any])->Void){
        
       var currentWirelessInfo: [String: Any] = [:]
        
        if #available(iOS 14.0, *) {
            
            NEHotspotNetwork.fetchCurrent { network in
                
                guard let network = network else {
                    compleationHandler([:])
                    return
                }
                
                let bssid = network.bssid
                let ssid = network.ssid
                currentWirelessInfo = ["BSSID ": bssid, "SSID": ssid, "SSIDDATA": "<54656e64 615f3443 38354430>"]
                compleationHandler(currentWirelessInfo)
            }
        }
        else {
            #if !TARGET_IPHONE_SIMULATOR
            guard let interfaceNames = CNCopySupportedInterfaces() as? [String] else {
                compleationHandler([:])
                return
            }
            
            guard let name = interfaceNames.first, let info = CNCopyCurrentNetworkInfo(name as CFString) as? [String: Any] else {
                compleationHandler([:])
                return
            }
            
            currentWirelessInfo = info
            
            #else
            currentWirelessInfo = ["BSSID ": "c8:3a:35:4c:85:d0", "SSID": "Tenda_4C85D0", "SSIDDATA": "<54656e64 615f3443 38354430>"]
            #endif
            compleationHandler(currentWirelessInfo)
        }
    }

 

posted @ 2022-09-25 00:23  仲长可倾  阅读(511)  评论(0编辑  收藏  举报