ViewController.swift 3.4 KB

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