I'm trying to monitor the network access of an iOS app.
When I run the simulator for a first time with wifi turned ON I get the right message "We're connected!".
But when I switch OFF the wifi I get the same message "We're connected!".
If I switch it on again I get the "No connection." message.
If I continue to switch the network on and off I get the opposite status of the actual state of the wifi.
Any ideas what am I doing wrong? Here's the code:
import Foundation
import Network
final class NetworkMonitor: ObservableObject {
let queue = DispatchQueue(label: "NetworkMonitor")
let monitor = NWPathMonitor()
init() {
monitor.pathUpdateHandler = { path in
if path.status == .satisfied {
print("We're connected!")
} else {
print("No connection.")
}
}
monitor.start(queue: queue)
}
}