iOS 使用 Alamofire 實時監測網路狀況
阿新 • • 發佈:2019-02-11
最近在用Swift寫工程,,網路請求用的是Alamofire,在翻看這個庫的時候發現 NetworkReachabilityManager 可以進行網路監察,廢話不多說直接上程式碼吧,有興趣的朋友可以去深入研究(https://github.com/Alamofire/Alamofire.git)
let manager = NetworkReachabilityManager(host: "https://github.com/Alamofire/Alamofire.git") func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. manager!.listener = { status in switch status { case .notReachable: print("notReachable") case .unknown: print("unknown") case .reachable(.ethernetOrWiFi): print("ethernetOrWiFi") case .reachable(.wwan): print("wwan") } } manager!.startListening() return true }