16

Trying to get the SSID of current device. I have found plenty of examples on how to do it however I am struggling with getting the CNCopySupportedInterfaces to autocomplete. I have 'import SystemConfiguration' at the top of my swift file but no success. Can't seem to figure out what I am doing wrong.

shreddish
  • 1,637
  • 8
  • 24
  • 38

4 Answers4

25

iOS 12

You must enable Access WiFi Information from capabilities.

Important 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. Documentation link

You need: import SystemConfiguration.CaptiveNetwork

Underneath the covers, CaptiveNetwork is a C header file (.h) that is within the SystemConfiguration framework:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/SystemConfiguration.framework/Headers/CaptiveNetwork.h

If you know Objective-C, this goes into more depth:

iPhone get SSID without private library

You have to use the awkward syntax to bridge from any pure C API, so the following is required:

for interface in CNCopySupportedInterfaces().takeRetainedValue() as! [String] {
    println("Looking up SSID info for \(interface)") // en0
    let SSIDDict = CNCopyCurrentNetworkInfo(interface).takeRetainedValue() as! [String : AnyObject]
    for d in SSIDDict.keys {
        println("\(d): \(SSIDDict[d]!)")
    }
}

ADDENDUM FOR SWIFT 2.2 and 3.0

The CFxxx datatypes are now bridged to native Objective-C runtime, eliminating the head-scratching retain calls. However, nullable pointers give rise to Optionals, so things don't get any shorter. At least, it's fairly clear what's going on, plus the nil helps us identify the simulator. The other answer uses an awful lot of bit-casting and unsafe operations which seems non-Swiftian, so I offer this.

func getInterfaces() -> Bool {
    guard let unwrappedCFArrayInterfaces = CNCopySupportedInterfaces() else {
        print("this must be a simulator, no interfaces found")
            return false
        }
        guard let swiftInterfaces = (unwrappedCFArrayInterfaces as NSArray) as? [String] else {
        print("System error: did not come back as array of Strings")
        return false
    }
    for interface in swiftInterfaces {
        print("Looking up SSID info for \(interface)") // en0
        guard let unwrappedCFDictionaryForInterface = CNCopyCurrentNetworkInfo(interface) else {
            print("System error: \(interface) has no information")
            return false
        }
        guard let SSIDDict = (unwrappedCFDictionaryForInterface as NSDictionary) as? [String: AnyObject] else {
        print("System error: interface information is not a string-keyed dictionary")
            return false
        }
        for d in SSIDDict.keys {
            print("\(d): \(SSIDDict[d]!)")
        }
    }
    return true
}

Output on success:

SSIDDATA: <57696c6d 79>

BSSID: 12:34:56:78:9a:bc

SSID: YourSSIDHere

BaseZen
  • 8,650
  • 3
  • 35
  • 47
  • what is takeRetainedValue() ? – Nilesh Aug 17 '16 at 10:01
  • It is for compatibility with old C APIs that do not use objects maintained by ARC. It brings the object into management under ARC so it will be automatically released when you no longer need it. http://stackoverflow.com/questions/29048826/when-to-use-takeunretainedvalue-or-takeretainedvalue-to-retrieve-unmanaged-o – BaseZen Aug 17 '16 at 15:16
  • Edited for Swift 2.2 – BaseZen Aug 18 '16 at 21:40
  • 1
    To get `CNCopySupportedInterfaces` to work in iOS 12, you'll need to enable the new "Access WiFi Information" entitlement. To do so, to go Project Settings -> Select your App Target -> "Capabilities" Tab. There is a switch to turn on "Access WiFi Information. Unfortunately, I had to delete my App and reinstall in order for the entitlement to take effect. It worked after that. Also, see. https://stackoverflow.com/questions/50767946/systemconfiguration-captivenetwork-doesnt-work-on-ios-12 – Drew H Sep 19 '18 at 20:06
10

In Swift 2.0 / iOS 9 the API CaptiveNetwork is (nearly) gone or depreciated. I contacted Apple regarding this problem and I thought we could (or should) use the NEHotspotHelper instead. I got a respond from Apple today: One should continue to use CaptiveNetwork and the two relevant APIs (even tough there marked depreciated):

 CNCopySupportedInterfaces
 CNCopyCurrentNetworkInfo

The user braime posted an updated code-snippet for this problem on Ray Wenderlich forums:

 let interfaces:CFArray! = CNCopySupportedInterfaces()
    for i in 0..<CFArrayGetCount(interfaces){
        let interfaceName: UnsafePointer<Void> 
          =  CFArrayGetValueAtIndex(interfaces, i)
        let rec = unsafeBitCast(interfaceName, AnyObject.self)
        let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)")
        if unsafeInterfaceData != nil {
            let interfaceData = unsafeInterfaceData! as Dictionary!
            currentSSID = interfaceData["SSID"] as! String
        } else {
            currentSSID = ""
        }
    }

Works perfect for me.

hibento
  • 784
  • 1
  • 6
  • 9
  • Swift 2 + Xcode 7.2.1 targeting configuration 'Debug' for architecture 'x86_64' using 'OS X 10.11' sdk complains about "Error:'CNCopyCurrentNetworkInfo' is unavailable" even though SystemConfiguration.CaptiveNetwork is imported as a framework. Any suggestions on how to get current SSID? – Willam Hill Feb 20 '16 at 21:46
  • Do we also need the provisioning profile from member center to access these frameworks? Are we also able to use these to reconnect a device to the network? – JMStudios.jrichardson Apr 07 '16 at 20:57
2

Swift:

import SystemConfiguration.CaptiveNetwork

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
        }
    }

Then print(currentSSIDs()), not working on simulator, only real devices.

Taken from https://forums.developer.apple.com/thread/50302

Juan Boero
  • 6,281
  • 1
  • 44
  • 62
1
func getInterfaces() -> String?  {
    var ssid: 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
                break
            }
        }
    }
    return ssid
}

In iOS 12 and up you will need to enable the Access WiFi Information capability for your app in order to get the ssid

ironRoei
  • 2,049
  • 24
  • 45