【问题标题】:CNCopyCurrentNetworkInfo not working with iOS 14CNCopyCurrentNetworkInfo 不适用于 iOS 14
【发布时间】:2020-07-28 08:42:41
【问题描述】:

我有一个使用 WIFI 连接到外部设备的应用程序,我曾经通过检查 WIFI SSID 来验证 iPhone 是否已连接到设备。这在 iOS 13 发布时被阻止,我通过请求位置权限来获取 SSID 来修复它。

我现在尝试使用启用了定位服务的 iOS 14 测试版,但无法获取 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 测试版,但只是想问您,您成功运行了 iOS 13 的哪个子版本?我有一个基于 CNCopyCurrentNetworkInfo 13.1 的工作代码,但未能在 iOS 13.3 上运行相同的代码。以下是我修复并使其在 iOS 13.3 - 13.6 上运行的方法。 stackoverflow.com/a/60973304/13082295 iOS 14 有条件的时候我会试试。
  • 我现有的代码与您在解决方案中描述的完全一样,它在所有情况下都可以在 iOS 13 上运行,但对于 iOS 14,它只有在用户接受了位置访问且具有精确位置的情况下才有效
  • 您确实需要“精确位置”才能在 iOS 14 中请求任何 WiFi / SSID 相关信息。
  • @hibento 那么为什么它在 iOS 14 中不起作用?有什么想法吗?
  • @shaqirsaiyed 您需要“精确位置”权限,最后还需要“本地网络访问”权限 - developer.apple.com/videos/play/wwdc2020/10110

标签: ios wifi ios14


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    ios14 已弃用 CNCopyCurrentNetworkInfo CNCopyCurrentNetworkInfo (CFStringRef interfaceName) API_DEPRECATED_WITH_REPLACEMENT("[NEHotspotNetwork fetchCurrentWithCompletionHandler:]", ios(4.1, API_TO_BE_DEPRECATED), macCatalyst(14.0, API_TO_BE_DEPRECATED))

    【讨论】:

    • 您有这方面的详细信息或参考资料吗?如果是,请分享
    【解决方案3】:

    请尝试使用 HotspotHelper supportedNetworkInterfaces。

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

    【讨论】:

      猜你喜欢
      • 2019-10-28
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 2014-04-06
      • 2018-11-27
      • 1970-01-01
      相关资源
      最近更新 更多