ViewController.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // ViewController.swift
  3. // ReachabilityMacSample
  4. //
  5. // Created by Reda Lemeden on 28/11/2015.
  6. // Copyright © 2015 Ashley Mills. All rights reserved.
  7. //
  8. import Cocoa
  9. import Reachability
  10. class ViewController: NSViewController {
  11. @IBOutlet weak var networkStatus: NSTextField!
  12. @IBOutlet weak var hostNameLabel: NSTextField!
  13. var reachability: Reachability?
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. view.wantsLayer = true
  17. view.layer?.backgroundColor = NSColor.whiteColor().CGColor
  18. // Start reachability without a hostname intially
  19. setupReachability(useHostName: false, useClosures: true)
  20. startNotifier()
  21. // After 5 seconds, stop and re-start reachability, this time using a hostname
  22. let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(UInt64(5) * NSEC_PER_SEC))
  23. dispatch_after(dispatchTime, dispatch_get_main_queue()) {
  24. self.stopNotifier()
  25. self.setupReachability(useHostName: true, useClosures: true)
  26. self.startNotifier()
  27. }
  28. }
  29. func setupReachability(useHostName useHostName: Bool, useClosures: Bool) {
  30. let hostName = "google.com"
  31. hostNameLabel.stringValue = useHostName ? hostName : "No host name"
  32. print("--- set up with host name: \(hostNameLabel.stringValue)")
  33. do {
  34. let reachability = try useHostName ? Reachability(hostname: hostName) : Reachability.reachabilityForInternetConnection()
  35. self.reachability = reachability
  36. } catch ReachabilityError.FailedToCreateWithAddress(let address) {
  37. networkStatus.textColor = NSColor.redColor()
  38. networkStatus.stringValue = "Unable to create\nReachability with address:\n\(address)"
  39. return
  40. } catch {}
  41. if (useClosures) {
  42. reachability?.whenReachable = { reachability in
  43. self.updateLabelColourWhenReachable(reachability)
  44. }
  45. reachability?.whenUnreachable = { reachability in
  46. self.updateLabelColourWhenNotReachable(reachability)
  47. }
  48. } else {
  49. NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)
  50. }
  51. }
  52. func startNotifier() {
  53. print("--- start notifier")
  54. do {
  55. try reachability?.startNotifier()
  56. } catch {
  57. networkStatus.textColor = NSColor.redColor()
  58. networkStatus.stringValue = "Unable to start\nnotifier"
  59. return
  60. }
  61. }
  62. func stopNotifier() {
  63. print("--- stop notifier")
  64. reachability?.stopNotifier()
  65. NSNotificationCenter.defaultCenter().removeObserver(self, name: ReachabilityChangedNotification, object: nil)
  66. reachability = nil
  67. }
  68. func updateLabelColourWhenReachable(reachability: Reachability) {
  69. print("\(reachability.description) - \(reachability.currentReachabilityString)")
  70. if reachability.isReachableViaWiFi() {
  71. self.networkStatus.textColor = NSColor.greenColor()
  72. } else {
  73. self.networkStatus.textColor = NSColor.blueColor()
  74. }
  75. self.networkStatus.stringValue = reachability.currentReachabilityString
  76. }
  77. func updateLabelColourWhenNotReachable(reachability: Reachability) {
  78. print("\(reachability.description) - \(reachability.currentReachabilityString)")
  79. self.networkStatus.textColor = NSColor.redColor()
  80. self.networkStatus.stringValue = reachability.currentReachabilityString
  81. }
  82. func reachabilityChanged(note: NSNotification) {
  83. let reachability = note.object as! Reachability
  84. if reachability.isReachable() {
  85. updateLabelColourWhenReachable(reachability)
  86. } else {
  87. updateLabelColourWhenNotReachable(reachability)
  88. }
  89. }
  90. }