Last active
February 25, 2024 01:39
-
-
Save mariosaputra/c656188bb3e3dac5351e6fb03ed64427 to your computer and use it in GitHub Desktop.
Check network status with swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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