I am working on an iOS app that requires an internet connection. I've noticed that the FaceBook app updates the screen to tell you when you have lost a connection. It also removes the warning when you regain your connection.
My question is how does this happen? Since the view is already loaded there must be some type of notification that is called if a connection is lost or recovered. Tonight I installed the popular Reachability code from GitHub and if I disconnect my connection once the view is loaded I am not notified that the connection was lost. I did sign up for a notification. The code is below.
override func viewWillAppear(animated: Bool)
{
super.viewWillAppear(animated)
var reachability: Reachability?
//declare this inside of viewWillAppear
do {
reachability = try Reachability.reachabilityForInternetConnection()
} catch {
print("Unable to create Reachability")
return
}
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(TipViewController.reachabilityChanged(_:)),name: ReachabilityChangedNotification,object: reachability)
do{
try reachability?.startNotifier()
}catch{
print("could not start reachability notifier")
}
}
func reachabilityChanged(note: NSNotification) {
let reachability = note.object as! Reachability
if reachability.isReachable() {
if reachability.isReachableViaWiFi() {
print("Reachable via WiFi")
} else {
print("Reachable via Cellular")
}
} else {
print("Network not reachable")
}
}
Any help would be appreciated.