Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dscyrescotti/reachabilityx
Make it easier to observe network connectivity in SwiftUI.
https://github.com/dscyrescotti/reachabilityx
connectivity networkconnection reachability swift swiftpackagemanager swiftui
Last synced: about 1 month ago
JSON representation
Make it easier to observe network connectivity in SwiftUI.
- Host: GitHub
- URL: https://github.com/dscyrescotti/reachabilityx
- Owner: dscyrescotti
- License: mit
- Created: 2021-09-12T06:53:04.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-09-26T09:02:41.000Z (about 3 years ago)
- Last Synced: 2024-09-14T08:25:51.295Z (2 months ago)
- Topics: connectivity, networkconnection, reachability, swift, swiftpackagemanager, swiftui
- Language: Swift
- Homepage:
- Size: 42 KB
- Stars: 9
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ReachabilityX
__ReachabilityX__ is built using `NWPathMonitor` from Apple's `Network` framework to provide an easy way to observe the network changes for SwiftUI.## Requirements
- iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+
- Swift 5.3+## Installation
### Swift Package Manager
Add it as a dependency within your `Package.swift`,
```
dependencies: [
.package(url: "https://github.com/dscyrescotti/ReachabilityX", from: "1.0.0")
]
```
Currently, **ReachabilityX** can only be installed via **Swift Package Manager**.## Usage
__ReachabilityX__ comes with `ReachabilityView` that exposes `Reachability` object supplied from its parent or ancestor. It is pretty handy to use when network changes are needed to observe across multiple views.
### Example 1
Firstly, you need to create an instance of `Reachability` inside the entry point of an app for the purpose of sharing it across multiple views via `environmentObject(_:)`.
```swift
@main
struct TestApp: App {
@ObservedObject var reachability: Reachability = .init()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(reachability)
.onAppear {
reachability.start()
}
}
}
}
```
Now, you can easily implement your presentation logic inside `ReachabilityView` and also implement your business logic using `onChangeStatus(action:)`, `onChangeInterfaceType(action:)` and `onChangePath(action:)`.
```swift
struct ContentView: View {
var body: some View {
ReachabilityView { (status: Status) in
// some presentation logic
}
.onChangeStatus { status in
// some business logic associated with status changes
}
.onChangeInterfaceType { interfaceType in
// some business logic associated with interface type changes
}
.onChangePath { path in
// some business logic associated with path changes
}
}
}
```
`ReachabilityView` comes with a couple of initializers that allows you to construct your desired views inside closures with different parameters.
```swift
// use it when you are only interested in status
ReachabilityView { (status: Status) in
// some presentation logic associated with status changes
}// use it when you are only interested in interface type
ReachabilityView { (interfaceType: InterfaceType) in
// some presentation logic associated with interface changes
}// use it when you are interested in both of status and interface type
ReachabilityView { (status: Status, interfaceType: InterfaceType) in
// some presentation logic associated with status and interface type changes
}// use it when you need to know the whole network path object
ReachabilityView { (path: NWPath) in
// some presentation logic associated with network path
}
```### Example 2
__ReachabiliyX__ also allows you to create your own `Reachability` instance by declaring inside SwiftUI view to observe the network changes.
```swift
struct ContentView: View {
@StateObject var reachability: Reachability = .init()
var body: some View {
Group {
switch reachability.status {
case .satisfied: Text("Connected!")
default: Text("Not Connected!")
}
}
.onAppear {
reachability.start()
}
.onDisappear {
reachability.stop()
}
.onChangeInterfaceType(reachability) { interfaceType in
// some business logic
}
}
}
```### Start and stop monitoring network changes
To obtain network path updates, you need to call `start()` method to begin observing network changes. If you want to stop observing changes, you have to call `stop()` method. This will no longer monitor any network changes.## Author
**Dscyre Scotti** (**[@dscyrescotti](https://twitter.com/dscyrescotti)**)## Contributions
**ReachabilityX** welcomes all developers to contribute if you have any idea to improve and open an issue if you find any kind of bug.
## License
ReachabilityX is available under the MIT license. See the [LICENSE](https://github.com/dscyrescotti/ReachabilityX/blob/main/LICENSE) file for more info.