ViewController.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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(hostName: nil, useClosures: true)
  18. startNotifier()
  19. // After 5 seconds, stop and re-start reachability, this time using a hostname
  20. let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(UInt64(5) * NSEC_PER_SEC))
  21. dispatch_after(dispatchTime, dispatch_get_main_queue()) {
  22. self.stopNotifier()
  23. self.setupReachability(hostName: "google.com", useClosures: true)
  24. self.startNotifier()
  25. let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(UInt64(5) * NSEC_PER_SEC))
  26. dispatch_after(dispatchTime, dispatch_get_main_queue()) {
  27. self.stopNotifier()
  28. self.setupReachability(hostName: "invalidhost", useClosures: true)
  29. self.startNotifier() }
  30. }
  31. }
  32. func setupReachability(hostName hostName: String?, useClosures: Bool) {
  33. hostNameLabel.text = hostName != nil ? hostName : "No host name"
  34. print("--- set up with host name: \(hostNameLabel.text!)")
  35. do {
  36. let reachability = try hostName == nil ? Reachability.reachabilityForInternetConnection() : Reachability(hostname: hostName!)
  37. self.reachability = reachability
  38. } catch ReachabilityError.FailedToCreateWithAddress(let address) {
  39. networkStatus.textColor = UIColor.redColor()
  40. networkStatus.text = "Unable to create\nReachability with address:\n\(address)"
  41. return
  42. } catch {}
  43. if (useClosures) {
  44. reachability?.whenReachable = { reachability in
  45. dispatch_async(dispatch_get_main_queue()) {
  46. self.updateLabelColourWhenReachable(reachability)
  47. }
  48. }
  49. reachability?.whenUnreachable = { reachability in
  50. dispatch_async(dispatch_get_main_queue()) {
  51. self.updateLabelColourWhenNotReachable(reachability)
  52. }
  53. }
  54. } else {
  55. NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)
  56. }
  57. }
  58. func startNotifier() {
  59. print("--- start notifier")
  60. do {
  61. try reachability?.startNotifier()
  62. } catch {
  63. networkStatus.textColor = UIColor.redColor()
  64. networkStatus.text = "Unable to start\nnotifier"
  65. return
  66. }
  67. }
  68. func stopNotifier() {
  69. print("--- stop notifier")
  70. reachability?.stopNotifier()
  71. NSNotificationCenter.defaultCenter().removeObserver(self, name: ReachabilityChangedNotification, object: nil)
  72. reachability = nil
  73. }
  74. func updateLabelColourWhenReachable(reachability: Reachability) {
  75. print("\(reachability.description) - \(reachability.currentReachabilityString)")
  76. if reachability.isReachableViaWiFi() {
  77. self.networkStatus.textColor = UIColor.greenColor()
  78. } else {
  79. self.networkStatus.textColor = UIColor.blueColor()
  80. }
  81. self.networkStatus.text = reachability.currentReachabilityString
  82. }
  83. func updateLabelColourWhenNotReachable(reachability: Reachability) {
  84. print("\(reachability.description) - \(reachability.currentReachabilityString)")
  85. self.networkStatus.textColor = UIColor.redColor()
  86. self.networkStatus.text = reachability.currentReachabilityString
  87. }
  88. func reachabilityChanged(note: NSNotification) {
  89. let reachability = note.object as! Reachability
  90. if reachability.isReachable() {
  91. updateLabelColourWhenReachable(reachability)
  92. } else {
  93. updateLabelColourWhenNotReachable(reachability)
  94. }
  95. }
  96. deinit {
  97. stopNotifier()
  98. }
  99. }