How to print the current local network name in swi

2019-09-22 03:19发布

问题:

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
}