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
In swift 2, you don't have to call
takeRetainedValue
.Replace the code
interfaces.takeRetainedValue() as [String : AnyObject]
withArray(arrayLiteral: interfaces)
.Also remember change the code
interfacesArray[0] as String
intoString(interfacesArray[0])
.Full code :
}
In Swift 3 (works in real device and simulator):
Usage:
The following function return the wifi name and the Mac address
This code works fine. You will note some warning in your development target >= iOS9
Swift 4 version
You can find an another answer with updated Swift standards of @japes answer which supports both Swift 3 and Swift 4. Return empty
""
on simulators.This is my solution Swift 3 iOS 10 and it works well with Xcode 8
To use it:
PS: Attention it not works on simulator