ViewController.swift 3.3 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. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. view.wantsLayer = true
  17. // Start reachability without a hostname intially
  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. <<<<<<< HEAD
  43. NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.reachabilityChanged(_:)), name: ReachabilityChangedNotification, object: reachability)
  44. =======
  45. NotificationCenter.default.addObserver(self, selector: #selector(ViewController.reachabilityChanged(_:)), name: ReachabilityChangedNotification, object: reachability)
  46. >>>>>>> feature/ios10
  47. }
  48. }
  49. func startNotifier() {
  50. print("--- start notifier")
  51. do {
  52. try reachability?.startNotifier()
  53. } catch {
  54. networkStatus.textColor = .red
  55. networkStatus.stringValue = "Unable to start\nnotifier"
  56. return
  57. }
  58. }
  59. func stopNotifier() {
  60. print("--- stop notifier")
  61. reachability?.stopNotifier()
  62. NotificationCenter.default.removeObserver(self, name: ReachabilityChangedNotification, object: nil)
  63. reachability = nil
  64. }
  65. func updateLabelColourWhenReachable(_ reachability: Reachability) {
  66. print("\(reachability.description) - \(reachability.currentReachabilityString)")
  67. if reachability.isReachableViaWiFi {
  68. self.networkStatus.textColor = .green
  69. } else {
  70. self.networkStatus.textColor = .blue
  71. }
  72. self.networkStatus.stringValue = reachability.currentReachabilityString
  73. }
  74. func updateLabelColourWhenNotReachable(_ reachability: Reachability) {
  75. print("\(reachability.description) - \(reachability.currentReachabilityString)")
  76. self.networkStatus.textColor = .red
  77. self.networkStatus.stringValue = reachability.currentReachabilityString
  78. }
  79. func reachabilityChanged(_ note: Notification) {
  80. let reachability = note.object as! Reachability
  81. if reachability.isReachable {
  82. updateLabelColourWhenReachable(reachability)
  83. } else {
  84. updateLabelColourWhenNotReachable(reachability)
  85. }
  86. }
  87. }