For swift 3.1 and Alamofire 4.4 ,I created a swift class called Connectivity
. Use NetworkReachabilityManager
class from Alamofire
and configure
the isConnectedToInternet()
method as per your need.
import Foundation
import Alamofire
class Connectivity {
class func isConnectedToInternet() -> Bool {
return NetworkReachabilityManager()?.isReachable ?? false
}
}
Usage:
if Connectivity.isConnectedToInternet() {
print("Yes! internet is available.")
// do some tasks..
}
EDIT:
Since swift is encouraging computed properties, you can change the above function like:
import Foundation
import Alamofire
class Connectivity {
class var isConnectedToInternet:Bool {
return NetworkReachabilityManager()?.isReachable ?? false
}
}
and use it like:
if Connectivity.isConnectedToInternet {
print("Yes! internet is available.")
// do some tasks..
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…