Make it easier to observe network connectivity in SwiftUI.

Overview

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(_:).

@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:).

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.

// 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.

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)

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 file for more info.

You might also like...
APIProvider - API Provider Package for easier API management inspired by abstraction

APIProvider Using APIProvider you can easily communicate with all API endpoints

iOS 15, MVVM, Async Await, Core Data, Abstract Network Layer, Repository & DAO design patterns, SwiftUI and Combine
iOS 15, MVVM, Async Await, Core Data, Abstract Network Layer, Repository & DAO design patterns, SwiftUI and Combine

iOS 15, MVVM, Async Await, Core Data, Abstract Network Layer, Repository & DAO design patterns, SwiftUI and Combine

Dratini is a neat network abstraction layer.

Dratini Dratini is a neat network abstraction layer. If you are looking for a solution to make your network layer neat, Dratini is your choice. Dratin

Network abstraction layer written in Swift.
Network abstraction layer written in Swift.

Moya 14.0.0 A Chinese version of this document can be found here. You're a smart developer. You probably use Alamofire to abstract away access to URLS

A toolkit for Network Extension Framework

NEKit NEKit is deprecated. It should still work but I'm not intent on maintaining it anymore. It has many flaws and needs a revamp to be a high-qualit

A lightweight, one line setup, iOS / OSX network debugging library! 🦊
A lightweight, one line setup, iOS / OSX network debugging library! 🦊

Netfox provides a quick look on all executed network requests performed by your iOS or OSX app. It grabs all requests - of course yours, requests from

Elegant network abstraction layer in Swift.
Elegant network abstraction layer in Swift.

Elegant network abstraction layer in Swift. δΈ­ζ–‡ Design Features Requirements Communication Installation Usage Base Usage - Target - Request - Download

Lightweight network abstraction layer, written on top of Alamofire
Lightweight network abstraction layer, written on top of Alamofire

TRON is a lightweight network abstraction layer, built on top of Alamofire. It can be used to dramatically simplify interacting with RESTful JSON web-

iOS network debugging, like a wizard πŸ§™β€β™‚οΈ
iOS network debugging, like a wizard πŸ§™β€β™‚οΈ

Start debugging iOS network calls like a wizard, without extra code! Wormholy makes debugging quick and reliable. What you can do: No code to write an

Releases(v1.0.0)
  • v1.0.0(Sep 26, 2021)

    What's new in v1.0.0?

    • Now, ReachabilityX is built using NWPathMonitor from Network framework to obtain network connectivity changes.
    • ReachabilityObservable is no longer available in version 1.0.0 and has been replaced with Reachability.
    • ReachabilityX provides the network status and interface type of the current network separately instead of combining them.
    • ReachabilityView comes with a couple of initializers that make it easier to implement the presentation logic.
    • onChangeConnection(_:) and onThrowError(_:) modifiers are no longer available and there comes with few new modifiers to implement the business logic.
    Source code(tar.gz)
    Source code(zip)
  • v0.1.0(Sep 21, 2021)

Owner
Dscyre Scotti
he/himΒ β€’ ios developer β€’ undergraduate student at UIT β€’ nerd
Dscyre Scotti
Modern networking support to monitor network connectivity

ConnectivityKit Adapting to changes in network connectivity can allow for suspending or resuming network activity. When entering an elevator or going

Brennan Stehling 1 Nov 7, 2022
🌐 Makes Internet connectivity detection more robust by detecting Wi-Fi networks without Internet access.

Connectivity is a wrapper for Apple's Reachability providing a reliable measure of whether Internet connectivity is available where Reachability alone

Ross Butler 1.6k Dec 30, 2022
An open-source Swift framework for building event-driven, zero-config Multipeer Connectivity apps

PeerKit An open-source Swift framework for building event-driven, zero-config Multipeer Connectivity apps Usage // Automatically detect and attach to

JP Simard 861 Dec 23, 2022
Control apps' wireless connectivity policy

AirKeeper This tweak replicates a feature in Chinese iPhone model by using Apple's own private APIs, which effectively writes to /var/preferences/com.

udevs 15 Dec 22, 2022
Another network wrapper for URLSession. Built to be simple, small and easy to create tests at the network layer of your application.

Another network wrapper for URLSession. Built to be simple, small and easy to create tests at the network layer of your application. Install Carthage

Ronan Rodrigo Nunes 89 Dec 26, 2022
A network extension app to block a user input URI. Meant as a network extension filter proof of concept.

URIBlockNE A network extension app to block a user input URI. Meant as a network extension filter proof of concept. This is just a research effort to

Charles Edge 5 Oct 17, 2022
Say goodbye to the Fat ugly singleton Network Manager with this Network Layer

MHNetwork Protocol Oriented Network Layer Aim to avoid having bloated singleton NetworkManager Philosophy the main philosophy behind MHNetwork is to h

Mohamed Emad Hegab 19 Nov 19, 2022
NWReachability - a pure Swift library for monitoring the network connection of iOS devices using Apple's Network framework.

NWReachability is a pure Swift library for monitoring the network connection of iOS devices using Apple's Network framework.

null 4 Dec 2, 2022
Easy and lightweight network layer for creating different set of network requests like GET, POST, PUT, DELETE customizable with coders conforming to TopLevelDecoder, TopLevelEncoder

Easy and lightweight network layer for creating different set of network requests like GET, POST, PUT, DELETE customizable with coders conforming to TopLevelDecoder, TopLevelEncoder

Igor 2 Sep 16, 2022
A tiny library makes uploading and downloading easier

Features uploading/downloading multiple files concurrently or sequentially grouping tasks with awesome custom operators (||| and -->) supports backgro

Le Van Nghia 450 Nov 19, 2022