I'm currently updating my application in regards to what is being returned from CNCopyCurrentNetworkInfo. I understand the privacy changes Apple implemented in regards to this starting on iOS 13 so i'm currently updating the implementation.
This is pretty straight forward. However the issue i'm running into is within this part of the app, the user will probably be in airplane mode (in-flight app). Regardless of the CLLocationManager.authorizationStatus(), even if it is .notDetermined which then triggers the requestWhenInUseAuthorization() method and once the user chooses either "Allow Once" or "Allow while Using App", i'm still not able to get the Wi-Fi ssid.
static func fetchSSIDInfo() -> String? {
if isSimulator() {
return "wireless"
} else {
if let interfaces: CFArray = CNCopySupportedInterfaces() {
for i in 0..<CFArrayGetCount(interfaces) {
let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interfaces, i)
let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
// skips this in airplane mode
if let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString) {
if let interfaceData = unsafeInterfaceData as Dictionary? {
let ssid = interfaceData["SSID" as NSObject] as? String
let bssid = interfaceData["BSSID" as NSObject] as? String
if ssid != "Wi-Fi" && bssid != "00:00:00:00:00:00" {
return ssid
} else {
return "invalid"
}
}
}
}
}
}
return nil
}
In the code above, when in airplane mode it actually skips the if let unsafeInterfaceData. When it isn't in airplane mode, it's working as expected and returns either the ssid or the invalid string depending if user allows location services.
My question is how am I able to get this working on airplane mode? Maybe i'm missing something, but at this point not too sure.
In iOS13, and possibly earlier versions (that I do not have immediately available to test on), once you enable "Airplane mode," the WiFi is automatically disconnected.
An end-user would need to proactively reenable WiFi on their device to reconnect, while still having Airplane mode enabled.
Your if
statement isn't getting executed, likely because there's no network information yet. Reenabling WiFi should get you the expected results.