2016-02-16 23 views
10

voglio una funzionalità simile a AFNetworking in Objective-C con Alamofire NetworkReachabilityManager a Swift:Come utilizzare NetworkReachabilityManager in Alamofire

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

Attualmente sto usando l'ascoltatore a conoscere i cambiamenti di stato con rete

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

Qualcuno può descrivere come supportare questi casi d'uso?

risposta

12

ho trovato la risposta io stesso cioè semplicemente scrivendo un ascoltatore con chiusura come indicato di seguito:

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") 

    } 
} 
14

Ecco la mia esecuzione. Lo uso in un singleton. Ricordarsi di conservare il riferimento del gestore di raggiungibilità.

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

NetworkManager Classe

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

Avviare Network raggiungibilità 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 
    } 
} 
+1

bel pezzo di codice – Abdoelrhman

4

Utilizzando un Singleton sta funzionando come a quando si mantiene un riferimento di 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() 
} 

Così si può usare in questo modo qualsiasi punto della app:

let networkStatus = NetworkStatus.sharedInstance 

override func awakeFromNib() { 
    super.awakeFromNib() 
    networkStatus.startNetworkReachabilityObserver() 
} 

Riceverai una notifica di qualsiasi modifica lo stato della rete. Solo per la ciliegina sulla torta this è un'animazione molto buona da mostrare sulla perdita della connessione internet.