如何在Alamofire中使用NetworkReachabilityManager

编程入门 行业动态 更新时间:2024-10-13 12:17:15
本文介绍了如何在Alamofire中使用NetworkReachabilityManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想要在Swift中使用Alamofire NetworkReachabilityManager在Objective-C中类似于 AFNetworking 的功能:

I want functionality similar to AFNetworking in Objective-C with Alamofire NetworkReachabilityManager in Swift:

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

我当前正在使用侦听器来了解网络状态的变化

I am currently using the listener to know the status changes with network

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

有人可以描述如何支持这些用例吗?

Can someone describe how to support those use cases?

推荐答案

我找到了答案我自己,例如,通过如下所述只写一个带有闭包的侦听器:

I found the answer myself i.e by just writing a listener with closure as mentioned below:

let net = NetworkReachabilityManager() 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") } } net?.startListening()

更多推荐

如何在Alamofire中使用NetworkReachabilityManager

本文发布于:2023-11-26 08:34:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633391.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Alamofire   NetworkReachabilityManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!