Skip to content

Instantly share code, notes, and snippets.

@dkw5877
Created January 12, 2019 22:24
Show Gist options
  • Save dkw5877/b3a6e0bc8f279bdcb5a373d61494d84b to your computer and use it in GitHub Desktop.
Save dkw5877/b3a6e0bc8f279bdcb5a373d61494d84b 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