ViewController.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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(useHostName: false, 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(useHostName: true, useClosures: true)
  24. self.startNotifier()
  25. }
  26. }
  27. func setupReachability(useHostName useHostName: Bool, useClosures: Bool) {
  28. let hostName = "google.com"
  29. hostNameLabel.text = useHostName ? hostName : "No host name"
  30. do {
  31. let reachability = try useHostName ? Reachability(hostname: hostName) : Reachability.reachabilityForInternetConnection()
  32. self.reachability = reachability
  33. } catch ReachabilityError.FailedToCreateWithAddress(let address) {
  34. networkStatus.textColor = UIColor.redColor()
  35. networkStatus.text = "Unable to create\nReachability with address:\n\(address)"
  36. return
  37. } catch {}
  38. if (useClosures) {
  39. reachability?.whenReachable = { reachability in
  40. self.updateLabelColourWhenReachable(reachability)
  41. }
  42. reachability?.whenUnreachable = { reachability in
  43. self.updateLabelColourWhenNotReachable(reachability)
  44. }
  45. } else {
  46. NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)
  47. }
  48. }
  49. func startNotifier() {
  50. do {
  51. try reachability?.startNotifier()
  52. } catch {
  53. networkStatus.textColor = UIColor.redColor()
  54. networkStatus.text = "Unable to start\nnotifier"
  55. return
  56. }
  57. }
  58. func stopNotifier() {
  59. reachability?.stopNotifier()
  60. NSNotificationCenter.defaultCenter().removeObserver(self, name: ReachabilityChangedNotification, object: nil)
  61. reachability = nil
  62. }
  63. func updateLabelColourWhenReachable(reachability: Reachability) {
  64. print("\(reachability.description) - \(reachability.currentReachabilityString)")
  65. if reachability.isReachableViaWiFi() {
  66. self.networkStatus.textColor = UIColor.greenColor()
  67. } else {
  68. self.networkStatus.textColor = UIColor.blueColor()
  69. }
  70. self.networkStatus.text = reachability.currentReachabilityString
  71. }
  72. func updateLabelColourWhenNotReachable(reachability: Reachability) {
  73. print("\(reachability.description) - \(reachability.currentReachabilityString)")
  74. self.networkStatus.textColor = UIColor.redColor()
  75. self.networkStatus.text = reachability.currentReachabilityString
  76. }
  77. func reachabilityChanged(note: NSNotification) {
  78. let reachability = note.object as! Reachability
  79. if reachability.isReachable() {
  80. updateLabelColourWhenReachable(reachability)
  81. } else {
  82. updateLabelColourWhenNotReachable(reachability)
  83. }
  84. }
  85. deinit {
  86. stopNotifier()
  87. }
  88. }