How to print the current local network name in swift

327 Views Asked by At

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

There are 1 best solutions below

1
Animesh On

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
}