如何在 Alamofire 中使用 NetworkReachabilityManager

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

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

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

发布评论

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

>www.elefans.com

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