iOS 使用 Alamofire 实时监测网络状况

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jancywen/article/details/72385479

最近在用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
    }


猜你喜欢

转载自blog.csdn.net/jancywen/article/details/72385479