Skip to content

Instantly share code, notes, and snippets.

@alfonsotesauro
Forked from dkw5877/NWPathMonitor.swift
Created March 25, 2021 17:47
Show Gist options
  • Save alfonsotesauro/0fff963b283a13c1d641abcc606b6b44 to your computer and use it in GitHub Desktop.
Save alfonsotesauro/0fff963b283a13c1d641abcc606b6b44 to your computer and use it in GitHub Desktop.
let monitor = NWPathMonitor()
/* closure called when path changes */
let pathUpdateHandler = {(path:NWPath) in
let availableInterfaces = path.availableInterfaces
if !availableInterfaces.isEmpty {
//e.g. [ipsec4, en0, pdp_ip0]
let list = availableInterfaces.map { $0.debugDescription }.joined(separator: "\n")
}
var status = "undetermined"
switch path.status {
case .requiresConnection:
status = "requires connection"
case .satisfied:
status = "satisfied"
case .unsatisfied:
status = "unsatisfied"
}
}
/* set the closure */
monitor.pathUpdateHandler = pathUpdateHandler
/* create the queue */
let queue = DispatchQueue.init(label: "monitor queue", qos: .userInitiated)
/* start monitoring for changes */
monitor.start(queue: queue)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment