Skip to content

Instantly share code, notes, and snippets.

@StanislavK
Created September 1, 2017 15:41
Show Gist options
  • Save StanislavK/58c3881995ece122916f432947be9d7c to your computer and use it in GitHub Desktop.
Save StanislavK/58c3881995ece122916f432947be9d7c to your computer and use it in GitHub Desktop.
import UIKit
import SystemConfiguration.CaptiveNetwork
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
print("SSID: \(currentID(for: kCNNetworkInfoKeySSID))\nBSSID: \(currentID(for: kCNNetworkInfoKeyBSSID))")
}
}
extension ViewController {
// kCNNetworkInfoKeySSID NetworkInfo Dictionary key for SSID in CFString format
// kCNNetworkInfoKeyBSSID NetworkInfo Dictionary key for BSSID in CFString format
/// Get the current SSID or BSSID, if connected to Wi-FI, otherwise return "N/A" string
func currentID(for networkInfoDictionaryKey: CFString) -> String {
enum SupportedInterfacesQueryResult: String, CustomStringConvertible {
case notAvailable = "N/A"
var description: String {
return self.rawValue
}
}
guard let interfaceNames = CNCopySupportedInterfaces() as? [String] else {
return "\(SupportedInterfacesQueryResult.notAvailable)"
}
let foundKeys: [String] = interfaceNames.flatMap { name in
guard let info = CNCopyCurrentNetworkInfo(name as CFString) as? [String:AnyObject] else {
return nil
}
guard let id = info[networkInfoDictionaryKey as String] as? String else {
return nil
}
return id
}
return foundKeys.first ?? "\(SupportedInterfacesQueryResult.notAvailable)"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment