Hey I am very late to post the answer, but when I saw this question I remembered how long I've spent time to get one working code to check if network is available and finally found this code. Connectivity variable is a Bool which will give momentary network status when accessed, use of this code block in a class will give you real time network status. Its in swift hope someone will find it useful.
Thanks
func checkNetworkStatus(completion:@escaping (_ connected:Bool) ->())
{
let reachability = AFNetworkReachabilityManager.shared()
reachability.startMonitoring();
reachability.setReachabilityStatusChange({ (status) -> Void in
switch(status) {
case .unknown:
self.connectivity = false
completion(false)
case .notReachable:
self.connectivity = false
completion(false)
case .reachableViaWWAN:
self.connectivity = true
completion(true)
case .reachableViaWiFi:
self.connectivity = true
completion(true)
}
})
}
This code block can be used in your class as
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.initNetworkMonitoring { (status) in
// make necessary UI updates
}