With SCNetworkReachabilityCreateWithAddress now depreciated I'm having to use NWPathMonitor to determine if I have a good connection to access some HTML and JSON files. I have this working just fine if the network connection is down but if the network connection comes back up it detects an event but still shows the status as unsatisfied. I can't seem to capture or pickup that when I've got a good connection again so I can proceed with the right status. Here is my current code. Any suggestions would be appreciated:
import Network
import Combine
class NetworkStatus: ObservableObject {
static let shared = NetworkStatus()
private var monitor: NWPathMonitor?
private var queue = DispatchQueue(label: "NetworkMonitor")
@Published var isConnected: Bool = false
private init() {
monitor = NWPathMonitor()
startMonitoring()
}
deinit {
stopMonitoring()
}
func startMonitoring() {
monitor?.pathUpdateHandler = { [weak self] path in
DispatchQueue.main.async {
let status = path.status == .satisfied
self?.isConnected = status
print(">>> \(path.status)")
}
}
monitor?.start(queue: queue)
}
func stopMonitoring() {
monitor?.cancel()
monitor = nil
}
}