Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NetworkReachabilityManager in Alamofire

I want functionality similar to AFNetworking in Objective-C with Alamofire NetworkReachabilityManager in Swift:

//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;         }     } }]; 

I am currently using the listener to know the status changes with network

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

Can someone describe how to support those use cases?

like image 707
MuraliMohan Avatar asked Feb 16 '16 08:02

MuraliMohan


2 Answers

NetworkManager Class

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()    } } 

Start Network Reachability Observer

@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     } } 
like image 60
Kirit Vaghela Avatar answered Sep 18 '22 18:09

Kirit Vaghela


I found the answer myself i.e by just writing a listener with closure as mentioned below:

let net = NetworkReachabilityManager()  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")      } }  net?.startListening() 
like image 31
MuraliMohan Avatar answered Sep 19 '22 18:09

MuraliMohan