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
Then use this code in your class to get SSID. import UIKit import SystemConfiguration. CaptiveNetwork import CoreLocation class ViewController: UIViewController, CLLocationManagerDelegate { var locationManager = CLLocationManager() var currentNetworkInfos: Array<NetworkInfo>? { get { return SSID.
To do this, open System Preferences, and then click Network. Select Wi-Fi in the sidebar, and then click Advanced. In the Wi-Fi tab, you will see a list of networks. This list is mainly there so you can choose an order of preference for joining networks.
This may help you (tested on Swift 2):
import Foundation
import SystemConfiguration.CaptiveNetwork
public class SSID {
class func fetchSSIDInfo() -> String {
var currentSSID = ""
if let interfaces = 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
}
}
}
return currentSSID
}
}
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.
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
Swift4.2
public class SSID {
class func fetchSSIDInfo() -> String {
var currentSSID = ""
if let interfaces = CNCopySupportedInterfaces() {
for i in 0..<CFArrayGetCount(interfaces) {
let interfaceName: UnsafeRawPointer = CFArrayGetValueAtIndex(interfaces, i)
let rec = unsafeBitCast(interfaceName, to: AnyObject.self)
let unsafeInterfaceData = CNCopyCurrentNetworkInfo("\(rec)" as CFString)
if let interfaceData = unsafeInterfaceData as? [String: AnyObject] {
currentSSID = interfaceData["SSID"] as! String
let BSSID = interfaceData["BSSID"] as! String
let SSIDDATA = interfaceData["SSIDDATA"] as! String
debugPrint("ssid=\(currentSSID), BSSID=\(BSSID), SSIDDATA=\(SSIDDATA)")
}
}
}
return currentSSID
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With