A modern, type-safe, actor-based network path monitoring utility for Apple platforms.
NetworkPathMonitor provides an easy and safe way to observe network connectivity changes using Swift Concurrency, AsyncStream, callbacks, and notifications.
- 🚦 Real-time network status monitoring based on
NWPathMonitor - 🧑💻 Actor isolation for thread safety (Swift Concurrency)
- 🌀 AsyncStream support for async/await style observation
- 🛎️ Callback and NotificationCenter support
- ⏳ Debounce mechanism to avoid frequent updates
- 🚫 Option to ignore the first path update
- 🛠️ Simple API, easy integration
- Swift 5.10 or later
- iOS 13.0+, macOS 10.15+, tvOS 13.0+, watchOS 6.0+, visionOS 1.0+
Add the following to your Package.swift:
.package(url: "https://github.com/codingiran/NetworkPathMonitor.git", from: "0.0.1")Or use Xcode:
File > Add Packages... and enter the repository URL.
import NetworkPathMonitor
let monitor = NetworkPathMonitor()
await monitor.fire()
// Check current status
let isConnected = await monitor.isPathSatisfied
// Stop monitoring
await monitor.invalidate()import NetworkPathMonitor
let monitor = NetworkPathMonitor()
await monitor.fire()
Task {
for await path in await monitor.pathUpdates {
print("Network status changed: \(path.status)")
}
}import NetworkPathMonitor
let monitor = NetworkPathMonitor()
await monitor.pathOnChange { path in
print("Network changed: \(path.status)")
}
await monitor.fire()import NetworkPathMonitor
let observer = NotificationCenter.default.addObserver(
forName: NetworkPathMonitor.networkStatusDidChangeNotification,
object: nil,
queue: .main
) { notification in
if let newPath = notification.userInfo?["newPath"] as? NWPath {
print("Network status changed to: \(newPath.status)")
}
}You can set a debounce interval to avoid frequent updates:
let monitor = NetworkPathMonitor(debounceInterval: 1.0) // 1 second debounceSometimes you may want to ignore the initial network path update that occurs when monitoring starts, especially during app launch. You can use the ignoreFirstPathUpdate parameter:
// Ignore the first path update when monitoring starts
let monitor = NetworkPathMonitor(ignoreFirstPathUpdate: true)
await monitor.fire() // First update will be ignored
// Useful for avoiding immediate notifications during app startup
let monitor = NetworkPathMonitor(
debounceInterval: 0.5,
ignoreFirstPathUpdate: true
)This is particularly useful when:
- You want to avoid showing network status alerts immediately when the app starts
- You only care about network changes after the initial connection is established
- You're implementing features that should only respond to actual network transitions
init(queue: DispatchQueue = ..., debounceInterval: TimeInterval = 0, ignoreFirstPathUpdate: Bool = false)queue: The dispatch queue for the underlying NWPathMonitor.debounceInterval: Debounce interval in seconds. Default is 0 (no debounce).ignoreFirstPathUpdate: Whether to ignore the first path update. Default is false.
isActive: Whether monitoring is active.currentPath: The latestNWPath.isPathSatisfied: Whether the current path is satisfied (connected).
fire(): Start monitoring.invalidate(): Stop monitoring.pathOnChange(_:): Register a callback for path changes.pathUpdates: AsyncStream of NWPath updates.
MIT License. See LICENSE for details.
Contributions are welcome! Please open issues or submit pull requests.