Find my current SSID in Swift 3 ?

Hello,


How do I find my SSID wifi swift 3?


Thank you for your help....


Bruno.

Replies

hey, you can do it with this code, it doesnt work in Simulator!!


func fetchSSIDInfo() ->  String {
        var currentSSID = ""
        if let interfaces:CFArray = CNCopySupportedInterfaces() {
            for i in 0..<CFArrayGetCount(interfaces){
                let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interfaces, i)
                let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
                let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString)
                if unsafeInterfaceData != nil {
                    let interfaceData = unsafeInterfaceData! as Dictionary!
                    for dictData in interfaceData! {
                        if dictData.key as! String == "SSID" {
                            currentSSID = dictData.value as! String
                        }
                    }
                }
            }
        }
        return currentSSID
    }

Here is a cleaner version, which doesn't cycle the entire array before returning:


     func fetchSSIDInfo() ->  String? {
        if let interfaces = CNCopySupportedInterfaces() {
            for i in 0..<CFArrayGetCount(interfaces){
                let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interfaces, i)
                let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
                let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString)
               
                if let unsafeInterfaceData = unsafeInterfaceData as? Dictionary<AnyHashable, Any> {
                    return unsafeInterfaceData["SSID"] as? String
                }
            }
        }
        return nil
    }

You’re definitely working way too hard here. The trick with using CF-based APIs from Swift is to get the data into ‘Swift space’ as quickly as possible. For example:

func currentSSIDs() -> [String] {
    guard let interfaceNames = CNCopySupportedInterfaces() as? [String] else {
        return []
    }
    return interfaceNames.flatMap { name in
        guard let info = CNCopyCurrentNetworkInfo(name as CFString) as? [String:AnyObject] else {
            return nil
        }
        guard let ssid = info[kCNNetworkInfoKeySSID as String] as? String else {
            return nil
        }
        return ssid
    }
}

Note that this returns an array of names; how you handle the non-standard cases (no elements, more than one element) is up to you.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Will this work for tvOS as well? It always returns nil for me. I have tested on the Apple TV.

Will this work for tvOS as well?

I’m quite surprised that the

<SystemConfiguration/CaptiveNetwork.h>
header is even present in the tvOS SDK. That header represents a captive network technology that was replaced twice before the first version of tvOS shipped (once via the semi-private captive network plug-ins, and again with NEHotspotHelper).

I recommend that you file a bug about this. Either that API should be made to work on tvOS, or it should be marked up to indicate that it’s not available on tvOS.

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Is there a macOS equivalent of this, since CNCopyCurrentNetworkInfo is available only on iOS and tvOS?

macOS has CoreWLAN, a comprehensive public API for interacting with Wi-Fi. Here’s how you’d implement my

currentSSIDs()
function using it.
import CoreWLAN

func currentSSIDs() -> [String] {
    let client = CWWiFiClient.shared()
    return client.interfaces()?.flatMap { interface in
        return interface.ssid()
    } ?? []
}

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi eskimo, I have a question about the BSSID,

I use the same methods as yours but I replaced "kCNNetworkInfoKeySSID" by "kCNNetworkInfoKeyBSSID".

And I have an unexpected result.

I received: ["b0:95:9e:9c:2a:6"] and should receive ["b0:95:9e:9c:2a:06"]


Do you have any clue about why the bssid isn't complete ?

Thx in advance ! have a good day !

Do you have any clue about why the bssid isn't complete ?

It seems that the only difference is that the BSSID is missing leading 0 digits. While that’s not something I do myself, it’s a relatively common practice for MAC addresses. If you need the leading 0, you’ll need to fix that yourself. For example:

func normalise(macString: String) -> String {
    return String(macString
        .uppercased()
        .split(separator: ":")
        .map{("00" + $0).suffix(2)}
        .joined(separator: ":")
    )
}
print(normalise(macString: "b0:95:9e:9c:2a:06"))    // prints: B0:95:9E:9C:2A:06
print(normalise(macString: "b0:95:9e:9c:2a:6"))     // prints: B0:95:9E:9C:2A:06

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks for your help !

Have a good day !

Hi eskimo,

I have a query. As we are reading the current SSID from code, hence is there a possibility that will this lead to app store rejection due to security concern.

Please open a new thread for this over in Core OS > Networking. This is an important question about policy, and I’d rather that we discuss this in its own thread so that we can separate it from all of the existing discussion about mechanics.

If you reply to this post with a link to your new thread, that’ll ensure that I find it.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Not working