奔五的人学iOS:swift获取连接的wifi名称及ap的mac地址

在网上搜索了半天,都是各种copy版本,更没有swift版本。经过多次试验终于搞定集成,与大家分享一下,以下代码在xcode7.1实现。

1、由于在swift中,需要导入系统库,所以还是需要一个桥接Header.h,在桥接文件中添加

#import <SystemConfiguration/CaptiveNetwork.h>

2、之后在swift文件中将以下函数添加即可

func getMAC()->(success:Bool,ssid:String,mac:String){
    
    if let cfa:NSArray = CNCopySupportedInterfaces() {
        for x in cfa {
            if let dict = CFBridgingRetain(CNCopyCurrentNetworkInfo(x as! CFString)) {
                let ssid = dict["SSID"]!
                let mac  = dict["BSSID"]!
                return (true,ssid as! String,mac as! String)
            }
        }
    }
    return (false,"","")
}

3、调用

    let x = getMAC()
    if x.success {
        print(x.mac)
        print(x.ssid)
    }


猜你喜欢

转载自blog.csdn.net/miw__/article/details/49863329