|
| 1 | +# NetworkPathMonitor |
| 2 | + |
| 3 | +[](https://swift.org) |
| 4 | +[](#requirements) |
| 5 | +[](LICENSE) |
| 6 | + |
| 7 | +A modern, type-safe, actor-based network path monitoring utility for Apple platforms. |
| 8 | +NetworkPathMonitor provides an easy and safe way to observe network connectivity changes using Swift Concurrency, AsyncStream, callbacks, and notifications. |
| 9 | + |
| 10 | +--- |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +- 🚦 Real-time network status monitoring based on `NWPathMonitor` |
| 15 | +- 🧑💻 Actor isolation for thread safety (Swift Concurrency) |
| 16 | +- 🌀 AsyncStream support for async/await style observation |
| 17 | +- 🛎️ Callback and NotificationCenter support |
| 18 | +- ⏳ Debounce mechanism to avoid frequent updates |
| 19 | +- 🛠️ Simple API, easy integration |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## Requirements |
| 24 | + |
| 25 | +- Swift 5.9 or later |
| 26 | +- iOS 13.0+, macOS 10.15+, tvOS 13.0+, watchOS 6.0+, visionOS 1.0+ |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## Installation |
| 31 | + |
| 32 | +### Swift Package Manager |
| 33 | + |
| 34 | +Add the following to your `Package.swift`: |
| 35 | + |
| 36 | +```swift |
| 37 | +.package(url: "https://github.com/codingiran/NetworkPathMonitor.git", from: "0.0.1") |
| 38 | +``` |
| 39 | + |
| 40 | +Or use Xcode: |
| 41 | +`File > Add Packages...` and enter the repository URL. |
| 42 | + |
| 43 | +--- |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +### Basic Usage |
| 48 | + |
| 49 | +```swift |
| 50 | +import NetworkPathMonitor |
| 51 | + |
| 52 | +let monitor = NetworkPathMonitor() |
| 53 | +await monitor.fire() |
| 54 | + |
| 55 | +// Check current status |
| 56 | +let isConnected = await monitor.isPathSatisfied |
| 57 | + |
| 58 | +// Stop monitoring |
| 59 | +await monitor.invalidate() |
| 60 | +``` |
| 61 | + |
| 62 | +### AsyncStream (Swift Concurrency) |
| 63 | + |
| 64 | +```swift |
| 65 | +import NetworkPathMonitor |
| 66 | + |
| 67 | +let monitor = NetworkPathMonitor() |
| 68 | +await monitor.fire() |
| 69 | + |
| 70 | +Task { |
| 71 | + for await path in await monitor.pathUpdates { |
| 72 | + print("Network status changed: \(path.status)") |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### Callback |
| 78 | + |
| 79 | +```swift |
| 80 | +import NetworkPathMonitor |
| 81 | + |
| 82 | +let monitor = NetworkPathMonitor() |
| 83 | +await monitor.pathOnChange { path in |
| 84 | + print("Network changed: \(path.status)") |
| 85 | +} |
| 86 | +await monitor.fire() |
| 87 | +``` |
| 88 | + |
| 89 | +### Notification |
| 90 | + |
| 91 | +```swift |
| 92 | +import NetworkPathMonitor |
| 93 | + |
| 94 | +let observer = NotificationCenter.default.addObserver( |
| 95 | + forName: NetworkPathMonitor.networkStatusDidChangeNotification, |
| 96 | + object: nil, |
| 97 | + queue: .main |
| 98 | +) { notification in |
| 99 | + if let oldPath = notification.userInfo?["oldPath"] as? NWPath, |
| 100 | + let newPath = notification.userInfo?["newPath"] as? NWPath { |
| 101 | + print("Network changed from \(oldPath.status) to \(newPath.status)") |
| 102 | + } |
| 103 | +} |
| 104 | +``` |
| 105 | + |
| 106 | +--- |
| 107 | + |
| 108 | +## Debounce |
| 109 | + |
| 110 | +You can set a debounce interval to avoid frequent updates: |
| 111 | + |
| 112 | +```swift |
| 113 | +let monitor = NetworkPathMonitor(debounceInterval: 1.0) // 1 second debounce |
| 114 | +``` |
| 115 | + |
| 116 | +--- |
| 117 | + |
| 118 | +## API |
| 119 | + |
| 120 | +### Initialization |
| 121 | + |
| 122 | +```swift |
| 123 | +init(queue: DispatchQueue = ..., debounceInterval: TimeInterval = 0) |
| 124 | +``` |
| 125 | + |
| 126 | +- `queue`: The dispatch queue for the underlying NWPathMonitor. |
| 127 | +- `debounceInterval`: Debounce interval in seconds. Default is 0 (no debounce). |
| 128 | + |
| 129 | +### Properties |
| 130 | + |
| 131 | +- `isActive`: Whether monitoring is active. |
| 132 | +- `currentPath`: The latest `NWPath`. |
| 133 | +- `isPathSatisfied`: Whether the current path is satisfied (connected). |
| 134 | + |
| 135 | +### Methods |
| 136 | + |
| 137 | +- `fire()`: Start monitoring. |
| 138 | +- `invalidate()`: Stop monitoring. |
| 139 | +- `pathOnChange(_:)`: Register a callback for path changes. |
| 140 | +- `pathUpdates`: AsyncStream of NWPath updates. |
| 141 | + |
| 142 | +--- |
| 143 | + |
| 144 | +## Extensions |
| 145 | + |
| 146 | +```swift |
| 147 | +extension NWPath { |
| 148 | + var isSatisfied: Bool { status == .satisfied } |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +## License |
| 155 | + |
| 156 | +MIT License. See [LICENSE](LICENSE) for details. |
| 157 | + |
| 158 | +--- |
| 159 | + |
| 160 | +## Contributing |
| 161 | + |
| 162 | +Contributions are welcome! Please open issues or submit pull requests. |
| 163 | + |
| 164 | +--- |
| 165 | + |
| 166 | +## Author |
| 167 | + |
| 168 | +[CodingIran](https://github.com/codingiran) |
0 commit comments