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
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Victor Lobe
  • 355
  • 3
  • 12
  • 2
    BTW - Using `if let` to cast to an optional makes no sense. And don't use `NSArray` or `NSDictionary` in Swift. – rmaddy Jun 08 '18 at 20:44
  • 6
    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."* – rmaddy Jun 08 '18 at 20:46
  • @rmaddy Oh ok, thanks, I will looking into this – Victor Lobe Jun 08 '18 at 20:47
  • @rmaddy Now I see – Victor Lobe Jun 08 '18 at 20:48
  • @VictorLobe Did that fix the problem? – stackunderflows Aug 06 '18 at 19:29

1 Answers1

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

Ckwanted
  • 470
  • 4
  • 7
  • 4
    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" – Drew H Sep 19 '18 at 19:15
  • 2
    I am not seeing this under the capabilities listed in Xcode 10. Any ideas? – Ryan Mendoza Sep 21 '18 at 20:59
  • 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. – Florian Burel Oct 02 '18 at 11:02
  • 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. – kwahn Oct 17 '18 at 18:29
  • 1
    "Wireless Accessory Configuration" is not "Access WiFi Information". Do not set the incorrect capability. – Harrison Xi Feb 21 '19 at 02:32
  • 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. – Ashwini Shahapurkar Jul 31 '20 at 17:47