ViewController.swift 3.3 KB

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