Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CNCopyCurrentNetworkInfo not working with iOS 14

Tags:

ios

wifi

ios14

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 the precise location is on when requesting location access

like image 841
Mahmoud Adam Avatar asked Jul 28 '20 08:07

Mahmoud Adam


2 Answers

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
   
}
like image 130
shaqir saiyed Avatar answered Sep 30 '22 08:09

shaqir saiyed


CNCopyCurrentNetworkInfo is deprecated for ios14 CNCopyCurrentNetworkInfo (CFStringRef interfaceName) API_DEPRECATED_WITH_REPLACEMENT("[NEHotspotNetwork fetchCurrentWithCompletionHandler:]", ios(4.1, API_TO_BE_DEPRECATED), macCatalyst(14.0, API_TO_BE_DEPRECATED))

like image 35
lufeich Avatar answered Sep 30 '22 09:09

lufeich