|
|
@@ -24,15 +24,15 @@ class ViewController: UIViewController {
|
|
|
startNotifier()
|
|
|
|
|
|
// After 5 seconds, stop and re-start reachability, this time using a hostname
|
|
|
- let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(UInt64(5) * NSEC_PER_SEC))
|
|
|
- dispatch_after(dispatchTime, dispatch_get_main_queue()) {
|
|
|
+ let dispatchTime = DispatchTime.now() + Double(Int64(UInt64(5) * NSEC_PER_SEC)) / Double(NSEC_PER_SEC)
|
|
|
+ DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
|
|
|
self.stopNotifier()
|
|
|
self.setupReachability(useHostName: true, useClosures: true)
|
|
|
self.startNotifier()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- func setupReachability(useHostName useHostName: Bool, useClosures: Bool) {
|
|
|
+ func setupReachability(useHostName: Bool, useClosures: Bool) {
|
|
|
let hostName = "google.com"
|
|
|
hostNameLabel.text = useHostName ? hostName : "No host name"
|
|
|
|
|
|
@@ -42,7 +42,7 @@ class ViewController: UIViewController {
|
|
|
let reachability = try useHostName ? Reachability(hostname: hostName) : Reachability.reachabilityForInternetConnection()
|
|
|
self.reachability = reachability
|
|
|
} catch ReachabilityError.FailedToCreateWithAddress(let address) {
|
|
|
- networkStatus.textColor = UIColor.redColor()
|
|
|
+ networkStatus.textColor = .red
|
|
|
networkStatus.text = "Unable to create\nReachability with address:\n\(address)"
|
|
|
return
|
|
|
} catch {}
|
|
|
@@ -55,7 +55,7 @@ class ViewController: UIViewController {
|
|
|
self.updateLabelColourWhenNotReachable(reachability)
|
|
|
}
|
|
|
} else {
|
|
|
- NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)
|
|
|
+ NotificationCenter.default.addObserver(self, selector: #selector(ViewController.reachabilityChanged(_:)), name: ReachabilityChangedNotification, object: reachability)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -64,7 +64,7 @@ class ViewController: UIViewController {
|
|
|
do {
|
|
|
try reachability?.startNotifier()
|
|
|
} catch {
|
|
|
- networkStatus.textColor = UIColor.redColor()
|
|
|
+ networkStatus.textColor = .red
|
|
|
networkStatus.text = "Unable to start\nnotifier"
|
|
|
return
|
|
|
}
|
|
|
@@ -73,34 +73,34 @@ class ViewController: UIViewController {
|
|
|
func stopNotifier() {
|
|
|
print("--- stop notifier")
|
|
|
reachability?.stopNotifier()
|
|
|
- NSNotificationCenter.defaultCenter().removeObserver(self, name: ReachabilityChangedNotification, object: nil)
|
|
|
+ NotificationCenter.default.removeObserver(self, name: ReachabilityChangedNotification, object: nil)
|
|
|
reachability = nil
|
|
|
}
|
|
|
|
|
|
- func updateLabelColourWhenReachable(reachability: Reachability) {
|
|
|
+ func updateLabelColourWhenReachable(_ reachability: Reachability) {
|
|
|
print("\(reachability.description) - \(reachability.currentReachabilityString)")
|
|
|
- if reachability.isReachableViaWiFi() {
|
|
|
- self.networkStatus.textColor = UIColor.greenColor()
|
|
|
+ if reachability.isReachableViaWiFi {
|
|
|
+ self.networkStatus.textColor = .green
|
|
|
} else {
|
|
|
- self.networkStatus.textColor = UIColor.blueColor()
|
|
|
+ self.networkStatus.textColor = .blue
|
|
|
}
|
|
|
|
|
|
self.networkStatus.text = reachability.currentReachabilityString
|
|
|
}
|
|
|
|
|
|
- func updateLabelColourWhenNotReachable(reachability: Reachability) {
|
|
|
+ func updateLabelColourWhenNotReachable(_ reachability: Reachability) {
|
|
|
print("\(reachability.description) - \(reachability.currentReachabilityString)")
|
|
|
|
|
|
- self.networkStatus.textColor = UIColor.redColor()
|
|
|
+ self.networkStatus.textColor = .red
|
|
|
|
|
|
self.networkStatus.text = reachability.currentReachabilityString
|
|
|
}
|
|
|
|
|
|
|
|
|
- func reachabilityChanged(note: NSNotification) {
|
|
|
+ func reachabilityChanged(_ note: Notification) {
|
|
|
let reachability = note.object as! Reachability
|
|
|
|
|
|
- if reachability.isReachable() {
|
|
|
+ if reachability.isReachable {
|
|
|
updateLabelColourWhenReachable(reachability)
|
|
|
} else {
|
|
|
updateLabelColourWhenNotReachable(reachability)
|