5

我有一个使用 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" }

知道如何解决这个问题吗?谢谢


更新:仅当请求位置访问时精确位置打开时才有效

4

3 回答 3

4

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


 

获取当前网络信息:

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

getNetworkInfo { (wifiInfo) in
               
  currentNetworkInfo = wifiInfo
   
}
于 2020-12-04T04:58:50.980 回答
1

ios14 不推荐使用 CNCopyCurrentNetworkInfo (CFStringRef interfaceName) API_DEPRECATED_WITH_REPLACEMENT("[NEHotspotNetwork fetchCurrentWithCompletionHandler:]", ios(4.1, API_TO_BE_DEPRECATED), macCatalyst(14.0, API_TO_BE_DEPRECATED))

于 2020-10-13T11:43:02.883 回答
0

请尝试使用 HotspotHelper 支持的网络接口。

https://developer.apple.com/documentation/networkextension/nehotspothelper/1618921-supportednetworkinterfaces

但我不明白为什么这段代码可以正常工作。我想将它用于我的应用程序,但我找不到使用此代码的开发人员。而且我找不到文件...

当位置权限被拒绝时,此代码有效。

注意:在使用 NEHotspotHelper 之前,您必须首先获得 Apple 的特殊权利 (com.apple.developer.networking.HotspotHelper)。

    NSArray<NEHotspotNetwork *> * currentNetwork = [NEHotspotHelper supportedNetworkInterfaces];
    NSString  *ssid = [NSString string];
    if(currentNetwork != nil && [currentNetwork count] > 0){
        ssid = currentNetwork[0].SSID;
    }
于 2020-11-30T07:08:35.807 回答