Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NWPathMonitor not calling satisfied in Swift

I'm using NWPathMonitorto detect when internet connection is on and off. The method gets called when both states happens, however when the internet connection is on, the called status still is .unsatisfiedinstead of .satisfied. Here's the class that I'm using:

import Network

protocol NetworkCheckDelegate {
    func statusDidChange(status: NWPath.Status)
}

class NetworkCheck {

    private var monitor = NWPathMonitor()

    private static let _sharedInstance = NetworkCheck()

    var networkCheckDelegate: NetworkCheckDelegate?

    class func sharedInstance() -> NetworkCheck {
        return _sharedInstance
    }

    // Create only one instance of NetworkCheck
    private init() {
        monitor.pathUpdateHandler = { path in
            DispatchQueue.main.async(execute: {
                self.networkCheckDelegate?.statusDidChange(status: path.status)
            })
        }
        monitor.start(queue: DispatchQueue.global(qos: .background))
    }

    func removeMonitoring() {
        monitor.cancel()
    }
}

My question is: Why does is the internet connection is called as .unsatisfiedin both cases

like image 590
Andrey Avatar asked Jul 26 '19 16:07

Andrey


1 Answers

The simulator cannot accurately transfer network changes to the application.

In my experience, it will be the network status of the device you are running Xcode on at launch, and only update itself when you go from having a connection (satisfied) to not (unsatisfied), but not the other way around.

You need to test network on real devices due to this limitation.

like image 65
thanat0sis Avatar answered Dec 01 '22 04:12

thanat0sis