How to Use Networkreachabilitymanager in Alamofire

How to use NetworkReachabilityManager in Alamofire

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 doesn't work with .notReachable

According to Alamofire "Make sure to remember to retain the manager" so create a manager like this

class A: UIViewController{

let manager = NetworkReachabilityManager(host: "www.apple.com")

override func viewDidLoad() {
super.viewDidLoad()

// before start listening you can check
if (manager?.isReachableOnEthernetOrWiFi == true)
{
print("internet is available")
}
else
{
print("internet is not available")
}

manager?.startListening()

manager?.listener = { status in

switch status {

case .notReachable:
print("network connection status - lost")
case .reachable(NetworkReachabilityManager.ConnectionType.ethernetOrWiFi):
print("network connection status - ethernet/WiFI")
case .reachable(NetworkReachabilityManager.ConnectionType.wwan):
print("network connection status - wwan")
default:
break
}
}

}

}

Attempt to check internet connection on iOS device with Alamofire

create the common class for check the connectivity

import Foundation
import Alamofire

class Connectivity {
class func isConnectedToInternet() -> Bool {
return NetworkReachabilityManager()!.isReachable
}
}

and call the function where you need

if !Connectivity.isConnectedToInternet() {
// show Alert
return
}

AFNetworking 5 - NetworkReachabilityManager Listener

Listener is just a typealias for the closure type expected, so you need to pass a closure.

self.reachabilityManager?.startListening { status in
switch status {
...
}
}

Checking for Internet Connection continually with Alamofire

As Jon Shier and Matt say you shouldn't do this. In fact if you are using Alamofire to download an image I suggest you to using instead AlamofireImage and use this code:

let url = URL(string: yourUrl)!

cell.yourImage.af_setImage(
withURL: url,
placeholderImage: placeholderImage,
imageTransition: .crossDissolve(0.2),
runImageTransitionIfCached: false,
completion: { response in
if response.result.isSuccess {
self.dismissLabel()
} else if response.error?._code == NSURLErrorNotConnectedToInternet{
self.showLabel()
}
})

So basically you can show a label "No internet Connection" when AlamofireImage retrieves a connection error while downloading your image. Instead if it succeeded to download it you dismiss the label.

Alamofire - detect internet connection

If you're declaring network inside a function it's probably being deallocated as soon as the scope ends.

Make sure you store it in an instance variable.



Related Topics



Leave a reply



Submit