5

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

Mahmoud Adam
  • 5,772
  • 5
  • 41
  • 62
  • 1
    You do need indeed 'precise location' to request any WiFi / SSID related information in iOS 14. – hibento Aug 31 '20 at 12:53
  • I've never tried iOS 14 beta but just wanted to ask you which sub version of iOS 13 have you managed to run? I had a working codes based on CNCopyCurrentNetworkInfo for 13.1 but failed to run the same on iOS 13.3. Below is how I fixed and make it running on iOS 13.3 - 13.6. https://stackoverflow.com/a/60973304/13082295 I'll try iOS 14 when I can source it. – Hayoung Yoon Aug 04 '20 at 04:05
  • My existing code is exactly as you described on your solution and it is working on iOS 13 in all cases but for iOS 14 it works only if the user accepted location access with precise location on – Mahmoud Adam Aug 04 '20 at 20:05
  • @hibento then why its not working in iOS 14 ? any idea ? – shaqir saiyed Oct 26 '20 at 09:31
  • 1
    @shaqirsaiyed you need 'precise location' permission and in the end also 'Local Network access' permission - https://developer.apple.com/videos/play/wwdc2020/10110/. – hibento Jan 17 '21 at 21:12

3 Answers3

4

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
   
}
shaqir saiyed
  • 732
  • 1
  • 10
  • 36
  • 1
    I know this is an old thread. This unfortunately does not work for me. I have `Access Wifi Information` capability enabled. NEHotspotNEtwork.fetchCurrent always return nil. iOS 15 – Jesper Kristiansen Nov 04 '22 at 00:15
1

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

lufeich
  • 11
  • 1
0

Please try using HotspotHelper supportedNetworkInterfaces.

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

But I don't understand why this code works exactly. I want to use it for my application, but I couldn't find a developer who use this code. And I couldn't find a document...

This code works when the location permssiion is rejected.

note : Before using NEHotspotHelper, you must first be granted a special entitlement (com.apple.developer.networking.HotspotHelper) by Apple.

    NSArray<NEHotspotNetwork *> * currentNetwork = [NEHotspotHelper supportedNetworkInterfaces];
    NSString  *ssid = [NSString string];
    if(currentNetwork != nil && [currentNetwork count] > 0){
        ssid = currentNetwork[0].SSID;
    }
uytt
  • 1
  • 1