7月 19, 2017

『Swift』讀取ios裝置當前所連上的Wifi SSID


倘若想要在自己的ios APP上讀取當前連到的Wifi名稱

就需要在該swift檔案中import以下函式庫



步驟一:
import SystemConfiguration.CaptiveNetwork

步驟二:
可以使用以下函式來取得Wifi名稱
func fetchSSIDInfo() ->  String? {
        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> {
                    return unsafeInterfaceData["SSID"] as? String
                }
            }
        }
        return nil
    }


沒有留言:

Related Posts Plugin for WordPress, Blogger...