Skip to content

Instantly share code, notes, and snippets.

@saeid-rez
Created May 22, 2022 17:06
Show Gist options
  • Select an option

  • Save saeid-rez/ce4dc200744f9c21a33427006325173c to your computer and use it in GitHub Desktop.

Select an option

Save saeid-rez/ce4dc200744f9c21a33427006325173c to your computer and use it in GitHub Desktop.
Network reachability helper with Combine and Alamofire
import Alamofire
import Combine
final class NetworkReachabilityHelper {
static let shared = NetworkReachabilityHelper()
private enum Constants {
static let host = "api1.\(Environment.domain)"
}
let networkStatus = PassthroughSubject<NetworkReachabilityManager.NetworkReachabilityStatus, Error>()
var reachability = NetworkReachabilityManager(host: Constants.host)
var isReachableOnCellular: Bool {
get {
return reachability?.isReachableOnCellular ?? false
}
}
var isReachableOnEthernetOrWiFi: Bool {
get {
return reachability?.isReachableOnEthernetOrWiFi ?? false
}
}
var isReachable: Bool {
get {
return reachability?.isReachable ?? false
}
}
func startListening() {
reachability?.startListening { [weak self] status in
self?.networkStatus.send(status)
}
}
func stopListening() {
reachability?.stopListening()
}
deinit {
stopListening()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment