SystemConfiguration.CaptiveNetwork doesn't work on iOS 12
Asked Answered
G

1

16

I have a function that detects the current SSID from the user. Unfortunately this doesn't work anymore with iOS 12. This means it just jumps over the if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? { part. Maybe it's just a bug or it's deprecated. I've found nothing on Apple Docs. On older iOS 11, 10, and 9 devices, it works well.

Here's my Code:

func getWiFiSsid() -> String? {
    if let interfaces = CNCopySupportedInterfaces() as NSArray? {
        for interface in interfaces {
            if let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? {

                ssid = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String


            }
        }
    }
    return ssid
}
Gorge answered 8/6, 2018 at 20:25 Comment(5)
BTW - Using if let to cast to an optional makes no sense. And don't use NSArray or NSDictionary in Swift.Sadden
Did you see this big note in the documentation for CNCopyCurrentNetworkInfo: "To use this function in iOS 12 and later, enable the Add WiFi Information capability for your app in Xcode. When you enable this capability, Xcode automatically adds the Add WiFi Information entitlement to your entitlements file and adds the corresponding feature to your App ID."Sadden
@Sadden Oh ok, thanks, I will looking into thisGorge
@Sadden Now I seeGorge
@VictorLobe Did that fix the problem?Monthly
R
37

To use this function in iOS 12 and later, enable the Access WiFi Information capability for your app in Xcode. When you enable this capability, Xcode automatically adds the Access WiFi Information entitlement to your entitlements file and App ID.

https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo?language=objc

enter image description here

Ribaudo answered 18/9, 2018 at 13:46 Comment(6)
To enable the Access WiFi Information capability, to go Project Settings -> Select your App Target -> "Capabilities" Tab. There is a switch to turn on "Access WiFi Information"Escuage
I am not seeing this under the capabilities listed in Xcode 10. Any ideas?Sikko
Note that to run it on a device, you need a provision profile for an AppID with this option enable, It cannot be a wildcard AppID.Brooklet
After doing this, I also had to also go to the developer console and enable Access WiFi Information for my App ID. Next I had to regenerate my provisioning profile as it was marked as "Invalid". After doing all that, I was able to get it working again.Rotten
"Wireless Accessory Configuration" is not "Access WiFi Information". Do not set the incorrect capability.Embolectomy
For this thing to work, please make sure that you request location authorization. The ssid info won't work unless app is authorized for using location.Cornfield

© 2022 - 2024 — McMap. All rights reserved.