Skip to content

Instantly share code, notes, and snippets.

@DavidBemerguy
Created June 7, 2023 09:01
Show Gist options
  • Select an option

  • Save DavidBemerguy/dad42579abde390c47730b5a98f08122 to your computer and use it in GitHub Desktop.

Select an option

Save DavidBemerguy/dad42579abde390c47730b5a98f08122 to your computer and use it in GitHub Desktop.

Revisions

  1. DavidBemerguy created this gist Jun 7, 2023.
    25 changes: 25 additions & 0 deletions PathMonitor.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    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)
    }
    }