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

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

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