Skip to content

Commit 92ed96f

Browse files
committed
Release 0.0.1
1 parent 63620b3 commit 92ed96f

7 files changed

Lines changed: 589 additions & 0 deletions

File tree

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc

Package.swift

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// swift-tools-version: 6.0
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "NetworkPathMonitor",
8+
platforms: [
9+
.iOS(.v13),
10+
.macOS(.v10_15),
11+
.macCatalyst(.v13),
12+
.tvOS(.v13),
13+
.watchOS(.v6),
14+
.visionOS(.v1),
15+
],
16+
products: [
17+
// Products define the executables and libraries a package produces, making them visible to other packages.
18+
.library(
19+
name: "NetworkPathMonitor",
20+
targets: ["NetworkPathMonitor"]
21+
),
22+
],
23+
targets: [
24+
// Targets are the basic building blocks of a package, defining a module or a test suite.
25+
// Targets can depend on other targets in this package and products from dependencies.
26+
.target(
27+
name: "NetworkPathMonitor"),
28+
.testTarget(
29+
name: "NetworkPathMonitorTests",
30+
dependencies: ["NetworkPathMonitor"]
31+
),
32+
],
33+
swiftLanguageModes: [.v6]
34+
)

[email protected]

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// swift-tools-version: 5.9
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "NetworkPathMonitor",
8+
platforms: [
9+
.iOS(.v13),
10+
.macOS(.v10_15),
11+
.macCatalyst(.v13),
12+
.tvOS(.v13),
13+
.watchOS(.v6),
14+
.visionOS(.v1),
15+
],
16+
products: [
17+
// Products define the executables and libraries a package produces, making them visible to other packages.
18+
.library(
19+
name: "NetworkPathMonitor",
20+
targets: ["NetworkPathMonitor"]
21+
),
22+
],
23+
targets: [
24+
// Targets are the basic building blocks of a package, defining a module or a test suite.
25+
// Targets can depend on other targets in this package and products from dependencies.
26+
.target(
27+
name: "NetworkPathMonitor"),
28+
.testTarget(
29+
name: "NetworkPathMonitorTests",
30+
dependencies: ["NetworkPathMonitor"]
31+
),
32+
],
33+
swiftLanguageVersions: [.v5]
34+
)

README.md

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# NetworkPathMonitor
2+
3+
[![Swift](https://img.shields.io/badge/Swift-6.0-orange.svg)](https://swift.org)
4+
[![Platforms](https://img.shields.io/badge/platforms-iOS%20%7C%20macOS%20%7C%20tvOS%20%7C%20watchOS%20%7C%20visionOS-lightgrey.svg)](#requirements)
5+
[![License](https://img.shields.io/badge/license-MIT-lightgrey.svg)](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)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Extensions.swift
3+
// NetworkPathMonitor
4+
//
5+
// Created by CodingIran on 2025/5/21.
6+
//
7+
8+
import Foundation
9+
import Network
10+
11+
// MARK: - Extensions
12+
13+
public extension Network.NWPath {
14+
var isSatisfied: Bool { status == .satisfied }
15+
}

0 commit comments

Comments
 (0)