ViewController.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 = Reachability(hostname: hostName)
  33. hostNameLabel.stringValue = hostName
  34. } else {
  35. reachability = 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(self, selector: #selector(ViewController.reachabilityChanged(_:)), name: .reachabilityChanged, object: reachability)
  49. }
  50. }
  51. func startNotifier() {
  52. print("--- start notifier")
  53. do {
  54. try reachability?.startNotifier()
  55. } catch {
  56. networkStatus.textColor = .red
  57. networkStatus.stringValue = "Unable to start\nnotifier"
  58. return
  59. }
  60. }
  61. func stopNotifier() {
  62. print("--- stop notifier")
  63. reachability?.stopNotifier()
  64. NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: nil)
  65. reachability = nil
  66. }
  67. func updateLabelColourWhenReachable(_ reachability: Reachability) {
  68. print("\(reachability.description) - \(reachability.connection.description)")
  69. if reachability.connection == .wifi {
  70. self.networkStatus.textColor = .green
  71. } else {
  72. self.networkStatus.textColor = .blue
  73. }
  74. self.networkStatus.stringValue = "\(reachability.connection)"
  75. }
  76. func updateLabelColourWhenNotReachable(_ reachability: Reachability) {
  77. print("\(reachability.description) - \(reachability.connection)")
  78. self.networkStatus.textColor = .red
  79. self.networkStatus.stringValue = "\(reachability.connection)"
  80. }
  81. @objc func reachabilityChanged(_ note: Notification) {
  82. let reachability = note.object as! Reachability
  83. if reachability.connection != .none {
  84. updateLabelColourWhenReachable(reachability)
  85. } else {
  86. updateLabelColourWhenNotReachable(reachability)
  87. }
  88. }
  89. }