RxReachability 1.2.1

RxReachability 1.2.1

测试已测试
语言语言 SwiftSwift
许可 MIT
发布最后发布2021年4月
SPM支持 SPM

RxReachability 维护者Joe MattRxSwift 社区 维护。



 
依赖
ReachabilitySwift< 6.0, >= 5.0
RxSwift~> 6
RxCocoa~> 6
 

  • 作者
  • Joseph Mattiello,Ivan Bruel,Bruno Oliveira 和 RxSwiftCommunity

Logo

RxReachability

GitHub release Version License Platform Build Status

RxReachability 为 ReachabilitySwift 添加了易于使用的 RxSwift 绑定。您可以响应网络可达性更改,在网络恢复时甚至可以重试可观察者。

可用的 API

RxReachability 添加以下 RxSwift 绑定

  • reachabilityChanged: Observable<Reachability>
  • status: Observable<Reachability.NetworkStatus>
  • isReachable: Observable<Bool>
  • isConnected: Observable<Void>
  • isDisconnected: Observable<Void>

常用示例

1. 请确保在您的ViewController或类似类中存储一个Reachability的实例,并在viewWillAppear和viewWillDisappear方法中启动/停止通知器。

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. 订阅任何绑定以知道何时发生更改。

extension ViewController {

  let disposeBag = DisposeBag()

  func bindReachability() {

    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)
  }

静态使用

1. 请确保在您的AppDelegate或类似类中的某个地方存储一个Reachability的实例,并启动通知器。

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. 订阅任何绑定以知道何时发生更改。

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)
  }

高级使用

使用RxReachability,您还可以在网络恢复正常时添加一个带有特定超时的重试。但这需要您存储一个Reachability的实例。

func request(somethingId: Int) -> Observable<Something> {
  return network.request(.something(somethingId))
    .retryOnConnect(timeout: 30)
    .mapSomething(JSON: $0) }
}

安装

CocoaPods安装

要在您的Xcode项目中使用CocoaPods集成RxReachability,请将以下行添加到您的Podfile

pod 'RxReachability'

示例

运行示例项目之前,请先克隆存储库,然后从示例目录运行pod install

许可证

此库属于 RxSwiftCommunity

RxReachability 遵循 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。