据我了解Alamofire是内置的Reachability,所以我自己的处理程序看起来像:
import Alamofire
let reachabilityManager = NetworkReachabilityManager()
reachabilityManager.listener = { status in
switch status {
case .notReachable:
print("The network is not reachable")
self.onInternetDisconnection()
case .unknown :
print("It is unknown whether the network is reachable")
self.onInternetDisconnection() // not sure what to do for this case
case .reachable(.ethernetOrWiFi):
print("The network is reachable over the WiFi connection")
self.onInternetConnection()
case .reachable(.wwan):
print("The network is reachable over the WWAN connection")
self.onInternetConnection()
}
}
我正在提出请求:
let provider = MoyaProvider<MyMoyaRequest>()
let token = provider.request(.start(
username:self.email.value,
password: self.password.value) { result in
switch result {
case let .success(moyaResponse):
//handle success
case let .failure(error):
//handle failure
}
}
因此,如果我想在发出每个Moya请求之前检查连接性,那么最好的方法是什么?
- 为 Moyas 内部的一个扩展编写一个扩展以首先检查
- 使用Moya插件(准备)检查
- 一些花哨的裤子到目前为止还没有想到……
出于可读性的原因,我特别不想为每个 API 调用添加可达性检查。但我有兴趣了解以前使用的方法。
感谢您提供的任何帮助。