Created
September 1, 2017 15:41
-
-
Save StanislavK/58c3881995ece122916f432947be9d7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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