Created
June 7, 2023 09:01
-
-
Save DavidBemerguy/dad42579abde390c47730b5a98f08122 to your computer and use it in GitHub Desktop.
Monitors network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Network | |
class PathMonitor: ObservableObject { | |
@Published private(set) var status: NWPath.Status | |
private let pathMonitor = NWPathMonitor() | |
private let pathMonitorQueue = DispatchQueue(label: "NWPathMonitor") | |
init(status: NWPath.Status = .unsatisfied, active: Bool = true) { | |
self.status = status | |
if active { | |
enablePathMonitor() | |
} | |
} | |
private func enablePathMonitor() { | |
pathMonitor.pathUpdateHandler = { path in | |
DispatchQueue.main.async { | |
self.status = path.status | |
} | |
} | |
pathMonitor.start(queue: pathMonitorQueue) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment