|
|
@@ -18,22 +18,22 @@ class ViewController: NSViewController {
|
|
|
override func viewDidLoad() {
|
|
|
super.viewDidLoad()
|
|
|
view.wantsLayer = true
|
|
|
- view.layer?.backgroundColor = NSColor.whiteColor().CGColor
|
|
|
+ view.layer?.backgroundColor = .white.cgColor
|
|
|
|
|
|
// Start reachability without a hostname intially
|
|
|
setupReachability(useHostName: false, useClosures: true)
|
|
|
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.stringValue = useHostName ? hostName : "No host name"
|
|
|
|
|
|
@@ -43,7 +43,7 @@ class ViewController: NSViewController {
|
|
|
let reachability = try useHostName ? Reachability(hostname: hostName) : Reachability.reachabilityForInternetConnection()
|
|
|
self.reachability = reachability
|
|
|
} catch ReachabilityError.FailedToCreateWithAddress(let address) {
|
|
|
- networkStatus.textColor = NSColor.redColor()
|
|
|
+ networkStatus.textColor = .red
|
|
|
networkStatus.stringValue = "Unable to create\nReachability with address:\n\(address)"
|
|
|
return
|
|
|
} catch {}
|
|
|
@@ -56,7 +56,7 @@ class ViewController: NSViewController {
|
|
|
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)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -65,7 +65,7 @@ class ViewController: NSViewController {
|
|
|
do {
|
|
|
try reachability?.startNotifier()
|
|
|
} catch {
|
|
|
- networkStatus.textColor = NSColor.redColor()
|
|
|
+ networkStatus.textColor = .red
|
|
|
networkStatus.stringValue = "Unable to start\nnotifier"
|
|
|
return
|
|
|
}
|
|
|
@@ -74,33 +74,33 @@ class ViewController: NSViewController {
|
|
|
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 = NSColor.greenColor()
|
|
|
+ if reachability.isReachableViaWiFi {
|
|
|
+ self.networkStatus.textColor = .green
|
|
|
} else {
|
|
|
- self.networkStatus.textColor = NSColor.blueColor()
|
|
|
+ self.networkStatus.textColor = .blue
|
|
|
}
|
|
|
|
|
|
self.networkStatus.stringValue = reachability.currentReachabilityString
|
|
|
}
|
|
|
|
|
|
- func updateLabelColourWhenNotReachable(reachability: Reachability) {
|
|
|
+ func updateLabelColourWhenNotReachable(_ reachability: Reachability) {
|
|
|
print("\(reachability.description) - \(reachability.currentReachabilityString)")
|
|
|
|
|
|
- self.networkStatus.textColor = NSColor.redColor()
|
|
|
+ self.networkStatus.textColor = .red
|
|
|
|
|
|
self.networkStatus.stringValue = 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)
|