ViewController.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // ViewController.swift
  3. // ReachabilityAppleTVSample
  4. //
  5. // Created by Stefan Schmitt on 10/12/15.
  6. // Copyright © 2015 Ashley Mills. All rights reserved.
  7. //
  8. import UIKit
  9. import Reachability
  10. class ViewController: UIViewController {
  11. @IBOutlet weak var networkStatus: UILabel!
  12. @IBOutlet weak var hostNameLabel: UILabel!
  13. var reachability: Reachability?
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. // Start reachability without a hostname intially
  17. setupReachability(useHostName: false, useClosures: true)
  18. startNotifier()
  19. // After 5 seconds, stop and re-start reachability, this time using a hostname
  20. let dispatchTime = DispatchTime.now() + Double(Int64(UInt64(5) * NSEC_PER_SEC)) / Double(NSEC_PER_SEC)
  21. DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
  22. self.stopNotifier()
  23. self.setupReachability(useHostName: true, useClosures: true)
  24. self.startNotifier()
  25. }
  26. }
  27. func setupReachability(useHostName: Bool, useClosures: Bool) {
  28. let hostName = "google.com"
  29. hostNameLabel.text = useHostName ? hostName : "No host name"
  30. print("--- set up with host name: \(hostNameLabel.text!)")
  31. let reachability = useHostName ? Reachability(hostname: hostName) : Reachability()
  32. self.reachability = reachability
  33. if useClosures {
  34. reachability?.whenReachable = { reachability in
  35. self.updateLabelColourWhenReachable(reachability)
  36. }
  37. reachability?.whenUnreachable = { reachability in
  38. self.updateLabelColourWhenNotReachable(reachability)
  39. }
  40. } else {
  41. NotificationCenter.default.addObserver(self, selector: #selector(ViewController.reachabilityChanged(_:)), name: ReachabilityChangedNotification, object: reachability)
  42. }
  43. }
  44. func startNotifier() {
  45. print("--- start notifier")
  46. do {
  47. try reachability?.startNotifier()
  48. } catch {
  49. networkStatus.textColor = .red
  50. networkStatus.text = "Unable to start\nnotifier"
  51. return
  52. }
  53. }
  54. func stopNotifier() {
  55. print("--- stop notifier")
  56. reachability?.stopNotifier()
  57. NotificationCenter.default.removeObserver(self, name: ReachabilityChangedNotification, object: nil)
  58. reachability = nil
  59. }
  60. func updateLabelColourWhenReachable(_ reachability: Reachability) {
  61. print("\(reachability.description) - \(reachability.currentReachabilityString)")
  62. if reachability.isReachableViaWiFi {
  63. self.networkStatus.textColor = .green
  64. } else {
  65. self.networkStatus.textColor = .blue
  66. }
  67. self.networkStatus.text = reachability.currentReachabilityString
  68. }
  69. func updateLabelColourWhenNotReachable(_ reachability: Reachability) {
  70. print("\(reachability.description) - \(reachability.currentReachabilityString)")
  71. self.networkStatus.textColor = .red
  72. self.networkStatus.text = reachability.currentReachabilityString
  73. }
  74. func reachabilityChanged(_ note: Notification) {
  75. let reachability = note.object as! Reachability
  76. if reachability.isReachable {
  77. updateLabelColourWhenReachable(reachability)
  78. } else {
  79. updateLabelColourWhenNotReachable(reachability)
  80. }
  81. }
  82. deinit {
  83. stopNotifier()
  84. }
  85. }