Jasmine Chaniara
Jasmine Chaniara

Reputation: 61

How to know in ios that with which network host the device is connected

How to know in ios that with which network host the device is connected. I want to learn in Swift that , how can we check that at which particular network we are connected.

Upvotes: 1

Views: 89

Answers (1)

Himanshu
Himanshu

Reputation: 2842

Use this custom class to get to the wifi network on which you are currently connected to :-

import Foundation
import SystemConfiguration.CaptiveNetwork

public class SSID {

    class func fetchSSIDInfo() -> String {

        var currentSSID = ""
        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 unsafeInterfaceData != nil {
                    let interfaceData = unsafeInterfaceData! as NSDictionary!
                    print(interfaceData)
                    currentSSID = interfaceData?.value(forKey:"SSID") as! String
                }
            }
        }
        return currentSSID
    }
}

You can get the Info this way :-

print(SSID.fetchSSIDInfo())

Upvotes: 1

Related Questions