CombineReachability

Version License Platform

CombineReachability 为 ReachabilitySwift 添加了易于使用的 Combine 发布者。 它增加了使用 Combine 的强大功能,以响应式方式对网络可达性变化做出反应的能力。 这个库的灵感来自于 RxSwift Community 的 RxReachability 库。

API

CombineReachability 添加了以下 Combine 发布者

常用方法

1. 确保存储一个 Reachability 实例,并通过调用 try? reachability?.startNotifier()reachability?.stopNotifier() 来分别启动/停止通知器。

class ViewController: UIViewController {

  var subscriptions = Set<AnyCancellable>()
  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 {

  func testReachability() {

    reachability?.reachabilityChanged
      .sink(receiveValue: { reachability: Reachability in
        print("Reachability changed: \(reachability.currrentReachabilityStatus)")
      })
      .store(in: &subscriptions)

    reachability?.status
      .sink(receiveValue: { status: Reachability.NetworkStatus in
        print("Reachability status changed: \(status)")
      })
      .store(in: &subscriptions)

    reachability?.isReachable
      .sink(receiveValue: { isReachable: Bool in
        print("Is reachable: \(isReachable)")
      })
      .store(in: &subscriptions)

    reachability?.isConnected
      .sink(receiveValue: {
        print("Is connected")
      })
      .store(in: &subscriptions)

    reachability?.isDisconnected
      .sink(receiveValue: {
        print("Is disconnected")
      })
      .store(in: &subscriptions)
  }

静态用法

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. 订阅任何发布者,以便了解何时发生可达性更改。

extension ViewController {

  func testReachability() {

    Reachability.reachabilityChanged
      .sink(receiveValue: { reachability: Reachability in
        print("Reachability changed: \(reachability.currrentReachabilityStatus)")
      })
      .store(in: &subscriptions)

    Reachability.status
      .sink(receiveValue: { status: Reachability.NetworkStatus in
        print("Reachability status changed: \(status)")
      })
      .store(in: &subscriptions)

    Reachability.isReachable
      .sink(receiveValue: { isReachable: Bool in
        print("Is reachable: \(isReachable)")
      })
      .store(in: &subscriptions)

    Reachability.isConnected
      .sink(receiveValue: {
        print("Is connected")
      })
      .store(in: &subscriptions)

    Reachability.isDisconnected
      .sink(receiveValue: {
        print("Is disconnected")
      })
      .store(in: &subscriptions)
  }

安装

通过 CocoaPods 安装

将以下行添加到您的 Podfile 并运行 pod install

pod 'CombineReachability'

通过 Swift Package Manager 安装

许可

CombineReachability 在 MIT 许可下可用。 有关更多信息,请参见 LICENSE 文件。