ViewController.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // Start reachability without a hostname initially
  18. setupReachability(useHostName: false, useClosures: true)
  19. startNotifier()
  20. // After 5 seconds, stop and re-start reachability, this time using a hostname
  21. let dispatchTime = DispatchTime.now() + Double(Int64(UInt64(5) * NSEC_PER_SEC)) / Double(NSEC_PER_SEC)
  22. DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
  23. self.stopNotifier()
  24. self.setupReachability(useHostName: true, useClosures: true)
  25. self.startNotifier()
  26. }
  27. }
  28. func setupReachability(useHostName: Bool, useClosures: Bool) {
  29. let hostName = "google.com"
  30. hostNameLabel.stringValue = useHostName ? hostName : "No host name"
  31. print("--- set up with host name: \(hostNameLabel.stringValue)")
  32. let reachability = useHostName ? Reachability(hostname: hostName) : Reachability()
  33. self.reachability = reachability
  34. if useClosures {
  35. reachability?.whenReachable = { reachability in
  36. self.updateLabelColourWhenReachable(reachability)
  37. }
  38. reachability?.whenUnreachable = { reachability in
  39. self.updateLabelColourWhenNotReachable(reachability)
  40. }
  41. } else {
  42. NotificationCenter.default.addObserver(self, selector: #selector(ViewController.reachabilityChanged(_:)), name: ReachabilityChangedNotification, object: reachability)
  43. }
  44. }
  45. func startNotifier() {
  46. print("--- start notifier")
  47. do {
  48. try reachability?.startNotifier()
  49. } catch {
  50. networkStatus.textColor = .red
  51. networkStatus.stringValue = "Unable to start\nnotifier"
  52. return
  53. }
  54. }
  55. func stopNotifier() {
  56. print("--- stop notifier")
  57. reachability?.stopNotifier()
  58. NotificationCenter.default.removeObserver(self, name: ReachabilityChangedNotification, object: nil)
  59. reachability = nil
  60. }
  61. func updateLabelColourWhenReachable(_ reachability: Reachability) {
  62. print("\(reachability.description) - \(reachability.currentReachabilityString)")
  63. if reachability.isReachableViaWiFi {
  64. self.networkStatus.textColor = .green
  65. } else {
  66. self.networkStatus.textColor = .blue
  67. }
  68. self.networkStatus.stringValue = reachability.currentReachabilityString
  69. }
  70. func updateLabelColourWhenNotReachable(_ reachability: Reachability) {
  71. print("\(reachability.description) - \(reachability.currentReachabilityString)")
  72. self.networkStatus.textColor = .red
  73. self.networkStatus.stringValue = reachability.currentReachabilityString
  74. }
  75. func reachabilityChanged(_ note: Notification) {
  76. let reachability = note.object as! Reachability
  77. if reachability.isReachable {
  78. updateLabelColourWhenReachable(reachability)
  79. } else {
  80. updateLabelColourWhenNotReachable(reachability)
  81. }
  82. }
  83. }