2016-02-16 13 views
10

でNetworkReachabilityManagerを使用する方法:私はスウィフトにAlamofire NetworkReachabilityManagerでのObjective-Cで<code>AFNetworking</code>と同様の機能をしたい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; 
     } 
    } 
}]; 

は、私は現在のネットワークとの状態の変化を知るために、リスナーを使用しています

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

誰かがそのユースケースをサポートする方法を記述できますか?

答えて

12

私は、後述するように、単に閉鎖してリスナーを書き込むことによって、すなわち自分自身に答えるが見つかりました:

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

ここに私の実装です。私はシングルトンで使っています。到達可能性マネージャの参照を保持することを忘れないでください。シングルトンを使用して

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のクラス

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

スタートネットワークの到達可能性オブザーバー

@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

素敵なコード – Abdoelrhman

4

私は長い間、あなたが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() 
} 

。 ちょうどケーキの氷結のためにthisあなたのインターネット接続の損失を示す非常に良いアニメーションです。

関連する問題