CNCopyCurrentNetworkInfo不适用于iOS 14 [英] CNCopyCurrentNetworkInfo not working with iOS 14

查看:311
本文介绍了CNCopyCurrentNetworkInfo不适用于iOS 14的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用WIFI连接到外部设备的应用程序,我过去通过检查WIFI SSID来验证iPhone是否已连接到该设备.在iOS 13发行时,此功能被阻止,我通过请求位置权限来获取SSID进行了修复.

我现在尝试在启用位置服务的iOS 14 beta上运行,但是无法获取WIFI SSID,但是相同的代码在iOS 13上也可以使用.

这是我致电CNCopyCurrentNetworkInfo

时获得的日志

nehelper sent invalid result code [1] for Wi-Fi information request

nehelper sent invalid response: <dictionary: 0x1e815f3e0> { count = 1, transaction: 0, voucher = 0x0, contents =
    "XPCErrorDescription" => <string: 0x1e815f548> { length = 18, contents = "Connection invalid" }
}

nehelper sent invalid response for Wi-Fi information request: <dictionary: 0x1e815f3e0> { count = 1, transaction: 0, voucher = 0x0, contents =
    "XPCErrorDescription" => <string: 0x1e815f548> { length = 18, contents = "Connection invalid" }

任何想法如何解决此问题? 谢谢


更新:仅在请求位置访问时启用精确位置时才有效

解决方案

我使用以下方法在iOS 14和更早版本中实现了这一目标.

导入NetworkExtension

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


 

获取当前的网络信息:

var currentNetworkInfo: [String: Any] = [:]

getNetworkInfo { (wifiInfo) in
               
  currentNetworkInfo = wifiInfo
   
}

I have an app that connects to an external device using WIFI and I used to validate that the iPhone is connected to the device by checking the WIFI SSID. This was blocked when iOS 13 was released and I fixed it by requesting location permission to get the SSID.

I tried now with iOS 14 beta with location service enabled but couldn't get the WIFI SSID, however the same code works with iOS 13.

Here is the log I get when I call CNCopyCurrentNetworkInfo

nehelper sent invalid result code [1] for Wi-Fi information request

nehelper sent invalid response: <dictionary: 0x1e815f3e0> { count = 1, transaction: 0, voucher = 0x0, contents =
    "XPCErrorDescription" => <string: 0x1e815f548> { length = 18, contents = "Connection invalid" }
}

nehelper sent invalid response for Wi-Fi information request: <dictionary: 0x1e815f3e0> { count = 1, transaction: 0, voucher = 0x0, contents =
    "XPCErrorDescription" => <string: 0x1e815f548> { length = 18, contents = "Connection invalid" }

Any idea how to solve this issue? Thanks


Update: it works only if precise location is on when requesting for location access

解决方案

I used following method to achieve this in iOS 14 and older versions.

import NetworkExtension

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


 

Get current network information :

var currentNetworkInfo: [String: Any] = [:]

getNetworkInfo { (wifiInfo) in
               
  currentNetworkInfo = wifiInfo
   
}

这篇关于CNCopyCurrentNetworkInfo不适用于iOS 14的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆