As far as I understand Alamofire is pulled in with built in Reachability, so my own handler would look something like:
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()
}
}
I'm making a request with:
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
}
}
So if I want to have connectivity checked before every Moya Request is made what is the best way to go about it?
- Write an extension for one of Moyas internals to check first
- Use the Moya plugin (prepare) to check
- Some fancy pants other way so far unthought of...
I specifically do not want to add a reachability check to every single API call, for readability reasons. But I am interested in hearing about methods previously used.
Thank-you for any assistance you can offer.