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