ViewController.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 = "googledcom"
  29. hostNameLabel.text = useHostName ? hostName : "No host name"
  30. print("--- set up with host name: \(hostNameLabel.text!)")
  31. do {
  32. let reachability = try useHostName ? Reachability(hostname: hostName) : Reachability.reachabilityForInternetConnection()
  33. self.reachability = reachability
  34. } catch ReachabilityError.FailedToCreateWithAddress(let address) {
  35. networkStatus.textColor = UIColor.redColor()
  36. networkStatus.text = "Unable to create\nReachability with address:\n\(address)"
  37. return
  38. } catch {}
  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. NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)
  48. }
  49. }
  50. func startNotifier() {
  51. print("--- start notifier")
  52. do {
  53. try reachability?.startNotifier()
  54. } catch {
  55. networkStatus.textColor = UIColor.redColor()
  56. networkStatus.text = "Unable to start\nnotifier"
  57. return
  58. }
  59. }
  60. func stopNotifier() {
  61. print("--- stop notifier")
  62. reachability?.stopNotifier()
  63. NSNotificationCenter.defaultCenter().removeObserver(self, name: ReachabilityChangedNotification, object: nil)
  64. reachability = nil
  65. }
  66. func updateLabelColourWhenReachable(reachability: Reachability) {
  67. print("\(reachability.description) - \(reachability.currentReachabilityString)")
  68. if reachability.isReachableViaWiFi() {
  69. self.networkStatus.textColor = UIColor.greenColor()
  70. } else {
  71. self.networkStatus.textColor = UIColor.blueColor()
  72. }
  73. self.networkStatus.text = reachability.currentReachabilityString
  74. }
  75. func updateLabelColourWhenNotReachable(reachability: Reachability) {
  76. print("\(reachability.description) - \(reachability.currentReachabilityString)")
  77. self.networkStatus.textColor = UIColor.redColor()
  78. self.networkStatus.text = reachability.currentReachabilityString
  79. }
  80. func reachabilityChanged(note: NSNotification) {
  81. let reachability = note.object as! Reachability
  82. if reachability.isReachable() {
  83. updateLabelColourWhenReachable(reachability)
  84. } else {
  85. updateLabelColourWhenNotReachable(reachability)
  86. }
  87. }
  88. deinit {
  89. stopNotifier()
  90. }
  91. }