CombineReachability
CombineReachability 为 ReachabilitySwift 添加了易于使用的 Combine 发布者。它通过 Combine 的强大功能以响应式方式对网络连接性变化做出响应。这个库受到了 RxSwift Community 的 RxReachability 库的启发。
APIs
CombineReachability 添加了以下 Combine 发布者
reachabilityChanged: AnyPublisher<Reachability, Never>
status: AnyPublisher<Reachability.Connection, Never>
isReachable: AnyPublisher<Bool, Never>
isConnected: AnyPublisher<Void, Never>
isDisconnected: AnyPublisher<Void, Never>
常见用法
Reachability
实例,并通过调用 try? reachability?.startNotifier()
和 reachability?.stopNotifier()
分别启动和停止通知器。
1. 确保存储一个 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)
}
静态使用
AppDelegate
或类似位置存储 Reachability
实例,并启动通知器。
1. 确保在您的 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包管理器安装
- 在Xcode中选择
文件/Swift 包/添加包依赖...
- 将
https://github.com/istvan-kreisz/CombineReachability.git
复制到仓库URL文本框中。
许可证
CombineReachability 遵循MIT许可证。更多信息请参阅LICENSE文件。