问题
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
回答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))
回答2:
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
}
回答3:
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;
}
来源:https://stackoverflow.com/questions/63130232/cncopycurrentnetworkinfo-not-working-with-ios-14