I am using the new "Network" library introduced in iOS 12, but I can't figure out why the network status does not get updated to .satisfied after user gets connected.
here is the code so far:
import Network
class MapViewController: UIViewController {
let networkMonitor = NWPathMonitor()
let queue = DispatchQueue(label: "NetworkMonitor")
override func viewDidLoad() {
super.viewDidLoad()
// check for internet connection
networkMonitor.pathUpdateHandler = { path in
if path.status != .satisfied {
// alert the user to check internet connection
let alert = UIAlertController(title: "Internet Error", message: "Unable to connect. Please check your internet connection.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))
alert.addAction(UIAlertAction(title: "Retry", style: .default, handler: { (action) in
// TODO: after retry should update status but its not updated
print("Status after retry: \(path.status)")
}))
self.present(alert, animated: true, completion: nil)
} else {
print(path.status)
}
}
networkMonitor.start(queue: queue)
}
}
To replicate the lost connection situation in simulator, I turn off wifi connection before the view loads, when the alert shows up, tap retry. Surprisingly Status after retry stays .unsatisfied. Why status does not get updated ?
The goal is to tap "Retry" and if user is still not connected keep showing the alert, when user gets connected and tap retry alert should be dismissed.
In order to have an easy network monitor setup you can check this tutorial: https://medium.com/@rwbutler/nwpathmonitor-the-new-reachability-de101a5a8835
Please note that I don't specify network type when instantiating NWPathMonitor(), therefore it detects all types of connections.