Im trying to use this code to get SSID
import Foundation
import SystemConfiguration.CaptiveNetwork
public class SSID {
class func getSSID() -> String{
var currentSSID = ""
let interfaces = CNCopySupportedInterfaces()
if interfaces != nil {
let interfacesArray = interfaces.takeRetainedValue() as [String : AnyObject]
if interfacesArray.count > 0 {
let interfaceName = interfacesArray[0] as String
let unsafeInterfaceData = CNCopyCurrentNetworkInfo(interfaceName)
if unsafeInterfaceData != nil {
let interfaceData = unsafeInterfaceData.takeRetainedValue() as Dictionary!
currentSSID = interfaceData[kCNNetworkInfoKeySSID] as! String
let ssiddata = NSString(data:interfaceData[kCNNetworkInfoKeySSIDData]! as! NSData, encoding:NSUTF8StringEncoding) as! String
// ssid data from hex
print(ssiddata)
}
}
}
return currentSSID
}
}
But in getting an error in this line
let interfacesArray = interfaces.takeRetainedValue() as [String : AnyObject]
The error is
Value of type 'CFArray?' has no member 'takeRetainedValue'
Thanks for your help
All the presented at the moment solutions seems rather complex, with ugly unsafe casts.
I was able to craft such a concise solution (with no black magic at all):
This may help you (tested on Swift 2):
I took and adapted the code from Ray Wenderlich's site (once was here: Retrieve SSID in iOS9 but now the specific thread has been removed from site)
iOS 12
You must enable Access WiFi Information from capabilities.
Swift4.2
https://stackoverflow.com/users/3108877/rob's answer -- shorter version