Skip to content

Instantly share code, notes, and snippets.

@mariosaputra
Last active February 25, 2024 01:39
Show Gist options
  • Select an option

  • Save mariosaputra/c656188bb3e3dac5351e6fb03ed64427 to your computer and use it in GitHub Desktop.

Select an option

Save mariosaputra/c656188bb3e3dac5351e6fb03ed64427 to your computer and use it in GitHub Desktop.
Check network status with swift
import Foundation
import Network
class NetworkMonitor: ObservableObject {
private let monitor: NWPathMonitor
private let queue = DispatchQueue(label: "NetworkMonitor")
@Published var isConnected: Bool = true
init() {
monitor = NWPathMonitor()
monitor.pathUpdateHandler = { [weak self] path in
DispatchQueue.main.async {
self?.isConnected = path.status == .satisfied
}
}
monitor.start(queue: queue)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment