如何在Alamofire中使用NetworkReachabilityManager

我想在Objective-C中使用类似于AFNetworkingfunction,在Swift中使用Alamofire NetworkReachabilityManager:

 //Reachability detection [[AFNetworkReachabilityManager sharedManager] startMonitoring]; [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { switch (status) { case AFNetworkReachabilityStatusReachableViaWWAN: { [self LoadNoInternetView:NO]; break; } case AFNetworkReachabilityStatusReachableViaWiFi: { [self LoadNoInternetView:NO]; break; } case AFNetworkReachabilityStatusNotReachable: { break; } default: { break; } } }]; 

我正在使用监听器来了解networking状态的变化

  let net = NetworkReachabilityManager() net?.startListening() 

有人可以描述如何支持这些用例吗?

NetworkManager类

 class NetworkManager { //shared instance static let shared = NetworkManager() let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.google.com") func startNetworkReachabilityObserver() { reachabilityManager?.listener = { status in switch status { case .notReachable: print("The network is not reachable") case .unknown : print("It is unknown whether the network is reachable") case .reachable(.ethernetOrWiFi): print("The network is reachable over the WiFi connection") case .reachable(.wwan): print("The network is reachable over the WWAN connection") } } // start listening reachabilityManager?.startListening() } } 

启动networking可达性观察器

 @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // add network reachability observer on app start NetworkManager.shared.startNetworkReachabilityObserver() return true } } 

这是我的实现。 我用它在一个单身人士。 记住要坚持可达性pipe理器的参考。

 let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.apple.com") func listenForReachability() { self.reachabilityManager?.listener = { status in print("Network Status Changed: \(status)") switch status { case .NotReachable: //Show error state case .Reachable(_), .Unknown: //Hide error state } } self.reachabilityManager?.startListening() } 
  I found the answer myself ie by just writing a listener with closure as mentioned below:- let net = NetworkReachabilityManager() net?.startListening() net?.listener = { status in if net?.isReachable ?? false { switch status { case .reachable(.ethernetOrWiFi): print("The network is reachable over the WiFi connection") case .reachable(.wwan): print("The network is reachable over the WWAN connection") case .notReachable: print("The network is not reachable") case .unknown : print("It is unknown whether the network is reachable") } } 

只要你保持对reachabilityManager的引用,使用单例就可以工作

 class NetworkStatus { static let sharedInstance = NetworkStatus() private init() {} let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.apple.com") func startNetworkReachabilityObserver() { reachabilityManager?.listener = { status in switch status { case .notReachable: print("The network is not reachable") case .unknown : print("It is unknown whether the network is reachable") case .reachable(.ethernetOrWiFi): print("The network is reachable over the WiFi connection") case .reachable(.wwan): print("The network is reachable over the WWAN connection") } } reachabilityManager?.startListening() } 

所以你可以在你的应用程序的任何地方使用它:

 let networkStatus = NetworkStatus.sharedInstance override func awakeFromNib() { super.awakeFromNib() networkStatus.startNetworkReachabilityObserver() } 

您的networking状态将发生变化。 只是锦上添花, 这是一个很好的animation,以显示您的互联网连接损失。