RxReachability is an open source project that I voluneteered to take ownership of in 2019.
It’s a small but vital project to many apps in the iOS community for providing a simple RxSwift bindable interface for use in funciontal code dependendant on network connectivity state.
This project is also served as a Cocoapod, the publishing of which is automated through GitHub actions I authored along with a suite of other CI/CD tools to assist with administrating such an important open-source project.
This project also support SwiftPM and Carthage.
RxReachability adds easy to use RxSwift bindings for ReachabilitySwift. You can react to network reachability changes and even retry observables when network comes back up.
RxReachability adds the following RxSwift bindings:
reachabilityChanged: Observable<Reachability>
status: Observable<Reachability.NetworkStatus>
isReachable: Observable<Bool>
isConnected: Observable<Void>
isDisconnected: Observable<Void>
Reachability
in your ViewController
or similar, and start/stop the notifier on viewWillAppear
and viewWillDisappear
methods.class ViewController: UIViewController {
let disposeBag = DisposeBag()
var reachability: Reachability?
override func viewDidLoad() {
super.viewDidLoad()
reachability = Reachability()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
try? reachability?.startNotifier()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
reachability?.stopNotifier()
}
}
extension ViewController {
let disposeBag = DisposeBag()
func bindReachability() {
reachability?.rx.reachabilityChanged
.subscribe(onNext: { reachability: Reachability in
print("Reachability changed: \(reachability.currentReachabilityStatus)")
})
.disposed(by: disposeBag)
reachability?.rx.status
.subscribe(onNext: { status: Reachability.NetworkStatus in
print("Reachability status changed: \(status)")
})
.disposed(by: disposeBag)
reachability?.rx.isReachable
.subscribe(onNext: { isReachable: Bool in
print("Is reachable: \(isReachable)")
})
.disposed(by: disposeBag)
reachability?.rx.isConnected
.subscribe(onNext: {
print("Is connected")
})
.disposed(by: disposeBag)
reachability?.rx.isDisconnected
.subscribe(onNext: {
print("Is disconnected")
})
.disposed(by: disposeBag)
}
Reachability
somewhere on your AppDelegate
or similar, and start the notifier.import Reachability
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var reachability: Reachability?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
reachability = Reachability()
try? reachability?.startNotifier()
return true
}
}
import Reachability
import RxReachability
import RxSwift
class ViewController: UIViewController {
let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
Reachability.rx.reachabilityChanged
.subscribe(onNext: { reachability: Reachability in
print("Reachability changed: \(reachability.currrentReachabilityStatus)")
})
.disposed(by: disposeBag)
Reachability.rx.status
.subscribe(onNext: { status: Reachability.NetworkStatus in
print("Reachability status changed: \(status)")
})
.disposed(by: disposeBag)
Reachability.rx.isReachable
.subscribe(onNext: { isReachable: Bool in
print("Is reachable: \(isReachable)")
})
.disposed(by: disposeBag)
Reachability.rx.isConnected
.subscribe(onNext: {
print("Is connected")
})
.disposed(by: disposeBag)
Reachability.rx.isDisconnected
.subscribe(onNext: {
print("Is disconnected")
})
.disposed(by: disposeBag)
}
With RxReachability you can also add a retry when network comes back up with a given timeout. This does require you to have a stored instance of Reachability though.
func request(somethingId: Int) -> Observable<Something> {
return network.request(.something(somethingId))
.retryOnConnect(timeout: 30)
.map { Something(JSON: $0) }
}
To integrate RxReachability into your Xcode project using CocoaPods, simply add the following line to your Podfile
:
pod 'RxReachability', ~> '1.2.1'
To integrate RxReachability into your Xcode project using CocoaPods, simply add the following line to your Cartfile
:
github RxSwiftCommunity/RxReachability ~> 1.2.1
To integrate RxReachability into your Xcode project using SPM, simply add the following line to your Package.swift
:
.package(url: "https://github.com/RxSwiftCommunity/RxReachability", .upToNextMajor(from: "1.2.1")),
To run the example project, clone the repo, and run pod install
from the Example directory first.
This library belongs to RxSwiftCommunity.
RxReachability is available under the MIT license. See the LICENSE file for more info. {“mode”:“full”,“isActive”:false}
Project link: https://github.com/rxswiftcommunity/rxreachability