Skip to content

Instantly share code, notes, and snippets.

@WingCH
Created June 23, 2019 09:20
Show Gist options
  • Save WingCH/e785b0efaffe251e043ba1e0bb50d1e6 to your computer and use it in GitHub Desktop.
Save WingCH/e785b0efaffe251e043ba1e0bb50d1e6 to your computer and use it in GitHub Desktop.
Get the Current WiFi AP Information

http://www.yuukinishiyama.com/2018/10/23/wifi-ap-info-swift4/

需要開發者帳號

會自動生成呢個 or 可能出現係info.plist

如果無整既話以下既code會出nil

import UIKit
import SystemConfiguration.CaptiveNetwork

struct NetworkInfo {
    public let interface:String
    public let ssid:String
    public let bssid:String
    init(_ interface:String, _ ssid:String,_ bssid:String) {
        self.interface = interface
        self.ssid = ssid
        self.bssid = bssid
    }
}

class NetworkInfos: NSObject {

    func getNetworkInfos() -> Array<NetworkInfo> {
        // https://forums.developer.apple.com/thread/50302
        guard let interfaceNames = CNCopySupportedInterfaces() as? [String] else {
            print("no interfaceNames")
            return []
        }
        
        let networkInfos:[NetworkInfo] = interfaceNames.compactMap{ name in
            print(name)
            guard let info = CNCopyCurrentNetworkInfo(name as CFString) as? [String:AnyObject] else {
                print("no info")
                return nil
            }
            guard let ssid = info[kCNNetworkInfoKeySSID as String] as? String else {
                print("no ssid")
                return nil
            }
            guard let bssid = info[kCNNetworkInfoKeyBSSID as String] as? String else {
                print("no bssid")
                return nil
            }
            return NetworkInfo(name, ssid,bssid)
        }
        return networkInfos
    }
}
//use
print(NetworkInfos.init().getNetworkInfos())

//result
[ou_fyp.NetworkInfo(interface: "en0", ssid: "OUHK-Student", bssid: "34:fc:b9:6c:41:d4")]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment