最近在用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
}
本文介绍如何使用Alamofire中的NetworkReachabilityManager来监测iOS应用的网络连接状态,并提供了具体的Swift代码示例。
5610

被折叠的 条评论
为什么被折叠?



