I try to make NWPathMonitor an observable object that returns true if there is a network connection and false if not.
Can you help me because my solution doesn't work.
Thanks
import Foundation
import Network
class TestNetStatus: ObservableObject {
let monitor = NWPathMonitor()
let queue = DispatchQueue.global(qos: .background)
@Published var connected: Bool = false
private var isConnected: Bool = false
init() {
monitor.start(queue: queue)
monitor.pathUpdateHandler = { path in
if path.status == .satisfied {
self.isConnected = true
} else {
self.isConnected = false
}
}
self.connected = isConnected
}
}