I would like to call a method when my iOS app starts, but I just want to call this method when there is connection. I have found that in Objective-C you can use Reachable
, but it turns out that this method is not part of Swift.
I have found a pod called Reachability.swift, and I am using the example that was provided:
override func viewWillAppear(animated: Bool) {
let reachability: Reachability
do {
reachability = try Reachability.reachabilityForInternetConnection()
} catch {
print("Unable to create Reachability")
return
}
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "reachabilityChanged:",
name: ReachabilityChangedNotification,
object: reachability)
do {
try reachability.startNotifier()
} catch {
print("This is not working.")
return
}
}
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("Not reachable")
}
}
However, this is not properly working. I works only when the I enter that ViewController, but not when I turn on and turn off the WiFi.