3

No one of the solutions found in this other question Get SSID in Swift 2 works because CaptiveNetwork framework was deprecated in Swift 2.0

In Swift 1.2 a use this function:

    func getSSID() -> String {

    let interfaces = CNCopySupportedInterfaces()
    if interfaces == nil {
        return ""
    }

    //let interfacesArray = interfaces.takeRetainedValue() as! [String]
    let interfacesArray = Array(arrayLiteral: interfaces)
    if interfacesArray.count <= 0 {
        return ""
    }

    let interfaceName = String(interfacesArray[0])
    let unsafeInterfaceData = CNCopyCurrentNetworkInfo(interfaceName)
    if unsafeInterfaceData == nil {
        return ""
    }

    let interfaceData = unsafeInterfaceData.takeRetainedValue() as Dictionary!
    print(interfaceData["SSID"], terminator: "")
    return interfaceData["SSID"] as! String
}

But the following code does not work anymore..

Community
  • 1
  • 1

1 Answers1

1

As far as I'm aware, the CaptiveNetwork APIs are deprecated in iOS 9, but still available — so you should still be able to use them (at your own peril, as future updates may cause them to no longer work as expected). If they aren't visible from Swift, you can make them so from an ObjC bridging header.

This isn't an area I work with much, but it looks like the new Network Extensions API is intended to replace CaptiveNetwork anyway. See Network Extension Framework Reference for docs and the WWDC15 session What's New in Network Extension and VPN.

rickster
  • 124,678
  • 26
  • 272
  • 326