ViewController.swift 3.8 KB

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