How to detect the switching to different wifi networks in iOS?

109 views Asked by At

I'm going through the issue in identifying network switching, let's assume I have connected to wifi-1 and then immediately switched to wifi-2. So, here is my problem that when I'm switching to wifi-2 vice versa I'm not getting updated. To get to know these status I have used Reachability and Network framework. But, still didn't found the solution.

Note: Few time I'm getting updated, when it takes time to switching to the next wifi.

Used the below code of Alamofire reachability

       NetworkReachabilityManager.default?.startListening(onUpdatePerforming: { status in
            switch status {
            case .notReachable:
                print("Off")
                DispatchQueue.main.async {
                    self.view.backgroundColor = .red
                }
            case .unknown:
                print("Unknown")
                DispatchQueue.main.async {
                    self.view.backgroundColor = .orange
                }
            case .reachable(let type):
                print("On - \(type)")
                DispatchQueue.main.async {
                    self.view.backgroundColor = .green
                }
            }
        })

Used the below code of Network

        let monitor = NWPathMonitor()
        monitor.start(queue: DispatchQueue.global(qos: .background))
        monitor.pathUpdateHandler = { path in
            if path.status == .satisfied {
                DispatchQueue.main.async {
                    self.view.backgroundColor = .green
                }
            }else{
                DispatchQueue.main.async {
                    self.view.backgroundColor = .red
                }
            }
        }

0

There are 0 answers