Skip to content

Instantly share code, notes, and snippets.

@dinotrnka
Created January 17, 2023 12:10
Show Gist options
  • Save dinotrnka/f845ce63b89eab7d5bbfeb0be9500d21 to your computer and use it in GitHub Desktop.
Save dinotrnka/f845ce63b89eab7d5bbfeb0be9500d21 to your computer and use it in GitHub Desktop.
import Foundation
import Network
class NetworkMonitor {
static let shared: NetworkMonitor = NetworkMonitor()
let monitor = NWPathMonitor()
private var status: NWPath.Status = .requiresConnection
var isReachable: Bool { status == .satisfied }
func startMonitoring() {
monitor.pathUpdateHandler = { path in
self.status = path.status
}
let queue = DispatchQueue(label: "Monitor")
monitor.start(queue: queue)
}
func stopMonitoring() {
monitor.cancel()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment