MuraliMohan MuraliMohan - 11 months ago 203
Swift Question

How to use NetworkReachabilityManager in Alamofire

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?

Answer Source

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

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

net?.listener = { status in
    if net?.isReachable ?? false {
        if (net?.isReachableOnEthernetOrWiFi) != nil {

        } else if (net?.isReachableOnWWAN)! {

        }
    } else {
        print("no connection")
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download