如何在 Alamofire 中使用 NetworkReachabilityManager [英] How to use NetworkReachabilityManager in Alamofire

查看:98
本文介绍了如何在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆