Reputation: 103
I'm trying to find out what SSID the machine is connected to. Everything I've found is for iOS and is useless. Using the code below, I get an error stating: "'CNCopyCurrentNetworkInfo' is unavailable" since it's iOS specific. What's the macOS equivalent?
Swift 3:
import SystemConfiguration.CaptiveNetwork
var wifiNetwork = "Unknown"
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 unsafeInterfaceData = unsafeInterfaceData as? Dictionary<AnyHashable, Any> {
wifiNetwork = (unsafeInterfaceData["SSID"] as? String)!
}
}
}
Upvotes: 1
Views: 1722
Reputation: 103
I used the code below to get the current SSID, after checking for internet connectivity.
import CoreWLAN
let wifiNetwork = CWWiFiClient.shared().interface()!.ssid()!
Upvotes: 3
Reputation: 11646
use answer from Quinn, the God of networking at Apple:
https://forums.developer.apple.com/thread/50302
copied here:
func currentSSIDs() -> [String] {
guard let interfaceNames = CNCopySupportedInterfaces() as? [String] else {
return []
}
return interfaceNames.flatMap { name in
guard let info = CNCopyCurrentNetworkInfo(name as CFString) as? [String:AnyObject] else {
return nil
}
guard let ssid = info[kCNNetworkInfoKeySSID as String] as? String else {
return nil
}
return ssid
}
}
Upvotes: 0