ViewController.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. let hostNames = [nil, "google.com", "invalidhost"]
  15. var hostIndex = 0
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. view.wantsLayer = true
  19. startHost(at: 0)
  20. }
  21. func startHost(at index: Int) {
  22. stopNotifier()
  23. setupReachability(hostNames[index], useClosures: true)
  24. startNotifier()
  25. DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
  26. self.startHost(at: (index + 1) % 3)
  27. }
  28. }
  29. func setupReachability(_ hostName: String?, useClosures: Bool) {
  30. let reachability: Reachability?
  31. if let hostName = hostName {
  32. reachability = try? Reachability(hostname: hostName)
  33. hostNameLabel.stringValue = hostName
  34. } else {
  35. reachability = try? Reachability()
  36. hostNameLabel.stringValue = "No host name"
  37. }
  38. self.reachability = reachability
  39. print("--- set up with host name: \(hostNameLabel.stringValue)")
  40. if useClosures {
  41. reachability?.whenReachable = { reachability in
  42. self.updateLabelColourWhenReachable(reachability)
  43. }
  44. reachability?.whenUnreachable = { reachability in
  45. self.updateLabelColourWhenNotReachable(reachability)
  46. }
  47. } else {
  48. NotificationCenter.default.addObserver(
  49. self,
  50. selector: #selector(ViewController.reachabilityChanged(_:)),
  51. name: .reachabilityChanged,
  52. object: reachability
  53. )
  54. }
  55. }
  56. func startNotifier() {
  57. print("--- start notifier")
  58. do {
  59. try reachability?.startNotifier()
  60. } catch {
  61. networkStatus.textColor = .red
  62. networkStatus.stringValue = "Unable to start\nnotifier"
  63. return
  64. }
  65. }
  66. func stopNotifier() {
  67. print("--- stop notifier")
  68. reachability?.stopNotifier()
  69. NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: nil)
  70. reachability = nil
  71. }
  72. func updateLabelColourWhenReachable(_ reachability: Reachability) {
  73. print("\(reachability.description) - \(reachability.connection.description)")
  74. if reachability.connection == .wifi {
  75. self.networkStatus.textColor = .green
  76. } else {
  77. self.networkStatus.textColor = .blue
  78. }
  79. self.networkStatus.stringValue = "\(reachability.connection)"
  80. }
  81. func updateLabelColourWhenNotReachable(_ reachability: Reachability) {
  82. print("\(reachability.description) - \(reachability.connection)")
  83. self.networkStatus.textColor = .red
  84. self.networkStatus.stringValue = "\(reachability.connection)"
  85. }
  86. @objc func reachabilityChanged(_ note: Notification) {
  87. let reachability = note.object as! Reachability
  88. if reachability.connection != .unavailable {
  89. updateLabelColourWhenReachable(reachability)
  90. } else {
  91. updateLabelColourWhenNotReachable(reachability)
  92. }
  93. }
  94. }