https://github.com/RxSwiftCommunity/RxReachability
RxSwift bindings for Reachability
https://github.com/RxSwiftCommunity/RxReachability
network reachability rxcocoa rxswift swift
Last synced: 5 months ago
JSON representation
RxSwift bindings for Reachability
- Host: GitHub
- URL: https://github.com/RxSwiftCommunity/RxReachability
- Owner: RxSwiftCommunity
- License: mit
- Created: 2017-03-22T18:10:46.000Z (about 8 years ago)
- Default Branch: develop
- Last Pushed: 2022-09-01T06:13:11.000Z (over 2 years ago)
- Last Synced: 2024-11-11T18:32:38.378Z (5 months ago)
- Topics: network, reachability, rxcocoa, rxswift, swift
- Language: Swift
- Size: 3.31 MB
- Stars: 283
- Watchers: 9
- Forks: 79
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
- awesome-rxswift - RxReachability
README

RxReachability
=========
[](https://github.com/RxSwiftCommunity/rxreachability/releases)
[](http://cocoapods.org/pods/RxReachability)
[](http://cocoapods.org/pods/RxReachability)
[](http://cocoapods.org/pods/RxReachability)
[](https://travis-ci.org/RxSwiftCommunity/RxReachability)
RxReachability adds easy to use RxSwift bindings for [ReachabilitySwift](https://github.com/ashleymills/Reachability.swift).
You can react to network reachability changes and even retry observables when network comes back up.## Available APIs
RxReachability adds the following RxSwift bindings:
- `reachabilityChanged: Observable`
- `status: Observable`
- `isReachable: Observable`
- `isConnected: Observable`
- `isDisconnected: Observable`## Common Usage
#### 1. Be sure to store an instance of `Reachability` in your `ViewController` or similar, and start/stop the notifier on `viewWillAppear` and `viewWillDisappear` methods.
```swift
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()
}
}```
#### 2. Subscribe to any of the bindings to know when a change happens.
```swift
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)
}
```## Static Usage
#### 1. Be sure to store an instance of `Reachability` somewhere on your `AppDelegate` or similar, and start the notifier.
```swift
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
}
}```
#### 2. Subscribe to any of the bindings to know when a change happens.
```swift
import Reachability
import RxReachability
import RxSwiftclass 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)
}
```## Advanced Usage
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.```swift
func request(somethingId: Int) -> Observable {
return network.request(.something(somethingId))
.retryOnConnect(timeout: 30)
.map {Â Something(JSON: $0) }
}
```## Installation
### Installation via CocoaPods
To integrate RxReachability into your Xcode project using CocoaPods, simply add the following line to your `Podfile`:
```ruby
pod 'RxReachability', '~> 1.2.1'
```### Installation via Carthage
To integrate RxReachability into your Xcode project using CocoaPods, simply add the following line to your `Cartfile`:
```ruby
github "RxSwiftCommunity/RxReachability" ~> 1.2.1
```### Installation via Swift Package Manager (SPM)
To integrate RxReachability into your Xcode project using SPM, simply add the following line to your `Package.swift`:
```swift
.package(url: "https://github.com/RxSwiftCommunity/RxReachability", .upToNextMajor(from: "1.2.1")),
```## Example
To run the example project, clone the repo, and run `pod install` from the Example directory first.
## License
This library belongs to _RxSwiftCommunity_.
RxReachability is available under the MIT license. See the LICENSE file for more info.