2017-07-18 87 views
0

我試圖找出該機器連接到的SSID。我發現的所有東西都是針對iOS的,並沒有用處。使用下面的代碼,我得到一個錯誤,指出:''CNCopyCurrentNetworkInfo'不可用「,因爲它是iOS特定的。什麼是macOS等價物?在Swift 3中檢索WiFi網絡

斯威夫特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)! 
      } 
     } 
    } 

回答

0

我用下面的代碼來獲取當前的SSID ,在檢查互聯網連接。

import CoreWLAN 

let wifiNetwork = CWWiFiClient.shared().interface()!.ssid()! 
0

使用回答:

https://forums.developer.apple.com/thread/50302

複製在這裏:

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 
    } 
} 
+0

CNCopyCurrentNetworkInfo僅可在IOS和tvOS根據[文檔】(https://developer.apple.com/documentation/systemconfiguration/1614126-cncopycurrentnetworkinfo)。我無法在任何地方找到macOS的模擬器。 – user2625833