Swift CNCopySupportedInterfaces invalid

Trying to get the SSID of the current device. I found many examples on how to do this, however I am afraid that CNCopySupportedInterfaces will be autocomplete. I have an "import SystemConfiguration" at the top of my quick file, but without success. I can't seem to understand what I'm doing wrong.

+11
ios swift
source share
4 answers

iOS 12

You must enable Access WiFi Information from the features.

Important. To use this feature in iOS 12 and later, enable the ability to access WiFi information for your application in Xcode. When you enable this feature, Xcode automatically adds the access right to WiFi information for your rights file and application ID. Documentation Link

You need: import SystemConfiguration.CaptiveNetwork

Under the covers, CaptiveNetwork is the C. (.h) header file, which is part of the SystemConfiguration:

/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 deep:

iPhone gets SSID without private library

You should use awkward syntax to migrate 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]!)") } } 

APPENDIX FOR SWIFT 2.2 and 3.0

CFxxx data CFxxx now CFxxx to the Objective-C native runtime, eliminating calls from retain head. However, pointers with a null value lead to the appearance of Options, so everything does not get shorter. At least it's pretty clear what is happening, plus nil helps us identify the simulator. Another answer uses a lot of bit-casting and unsafe operations that seem to be non-Swiftian, so I suggest 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 } 

Success Result:

SSIDDATA: <57696c6d 79>

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

SSID: YourSSIDHere

+21
source share

In Swift 2.0 / iOS 9, the CaptiveNetwork API has (almost) gone or depreciated. I contacted Apple about this issue, and I thought we could (or should) use NEHotspotHelper. I got an answer from Apple today: you need to continue to use CaptiveNetwork and two related APIs (even hard ones there are marked depreciated):

  CNCopySupportedInterfaces CNCopyCurrentNetworkInfo 

Braime posted an updated code snippet for this issue on the 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 = "" } } 

Great for me.

+9
source share

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 the simulator, is only real devices.

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

+1
source share
 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 above, you will need to enable access to Wi-Fi information for your application in order to get ssid

+1
source share

All Articles