How to print the current local network name in swift

让人想犯罪 __ 提交于 2019-12-13 09:47:35

问题


I am creating an iOS app which displays the current local network name at the top of the screen, and so forth. I am trouble-shooting different ways to display this but I can't manage the current program. Can someone help me out?

I've looked at several GitHub, stack overflow, and youtube comments about this, but nome of them worked.

In the current Xcode I'm using which is Xcode(10.4.2) I'm using a label(correct me if I should use something else) to display the current Wifi named --> (WiFi: ......)


回答1:


Please don't test on the simulator, use the iphone for testing.

Import SystemConfiguration :

import SystemConfiguration.CaptiveNetwork

In ViewDidLoad :

let wifiName = getWiFiName()
print("Wifi: \(String(describing: wifiName))")

Function :

func getWiFiName() -> String? {

    var serviceSetIdentifier:String?
    if let interfaces = CNCopySupportedInterfaces() as Array? {
        interfaces.forEach { interface in
            guard let interfaceInfo = CNCopyCurrentNetworkInfo(interface as! CFString) as NSDictionary? else { return }
                serviceSetIdentifier = interfaceInfo[kCNNetworkInfoKeySSID as String] as? String
            }
    }
    return serviceSetIdentifier
}


来源:https://stackoverflow.com/questions/57122688/how-to-print-the-current-local-network-name-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!