I'm using NWPathMonitor
to 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 .unsatisfied
instead 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 .unsatisfied
in both cases
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With