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
How to Print Boolean Flag in Nslog
Centering a View in Its Superview Using Visual Format Language
Could Not Insert New Outlet Connection: Could Not Find Any Information for the Class Named
How to Hide "-" (Delete) Button While Editing Uitableview
How to Call a View Controller Programmatically
Code Signing Is Required for Product Type Unit Test Bundle in Sdk iOS 8.0
How to Remove Border of the Navigationbar in Swift
iOS Nested View Controllers View Inside Uiviewcontroller's View
Xcode 10, Command Codesign Failed with a Nonzero Exit Code
Linking Child View Controllers to a Parent View Controller Within Storyboard
How to Add a Button to Uinavigationbar
Why Is There an Frame Rectangle and an Bounds Rectangle in an Uiview
iOS 7 Uisearchdisplaycontroller Search Bar Overlaps Status Bar While Searching
How to Properly Order Data from Firebase Chronologically
What Is a Monospace Font in iOS
Hide Device Volume Hud View While Adjusitng Volume with Mpvolumeview Slider