| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- //
- // ViewController.swift
- // Reachability Sample
- //
- // Created by Ashley Mills on 22/09/2014.
- // Copyright (c) 2014 Joylord Systems. All rights reserved.
- //
- import UIKit
- import Reachability
- class ViewController: UIViewController {
-
- @IBOutlet weak var networkStatus: UILabel!
- @IBOutlet weak var hostNameLabel: UILabel!
-
- var reachability: Reachability?
- let hostNames = [nil, "google.com", "invalidhost"]
- var hostIndex = 0
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- startHost(at: 0)
- }
-
- func startHost(at index: Int) {
- stopNotifier()
- setupReachability(hostNames[index], useClosures: true)
- startNotifier()
- DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
- self.startHost(at: (index + 1) % 3)
- }
- }
-
- func setupReachability(_ hostName: String?, useClosures: Bool) {
- let reachability: Reachability?
- if let hostName = hostName {
- reachability = try? Reachability(hostname: hostName)
- hostNameLabel.text = hostName
- } else {
- reachability = try? Reachability()
- hostNameLabel.text = "No host name"
- }
- self.reachability = reachability
- print("--- set up with host name: \(hostNameLabel.text!)")
- if useClosures {
- reachability?.whenReachable = { reachability in
- self.updateLabelColourWhenReachable(reachability)
- }
- reachability?.whenUnreachable = { reachability in
- self.updateLabelColourWhenNotReachable(reachability)
- }
- } else {
- NotificationCenter.default.addObserver(
- self,
- selector: #selector(reachabilityChanged(_:)),
- name: .reachabilityChanged,
- object: reachability
- )
- }
- }
-
- func startNotifier() {
- print("--- start notifier")
- do {
- try reachability?.startNotifier()
- } catch {
- networkStatus.textColor = .red
- networkStatus.text = "Unable to start\nnotifier"
- return
- }
- }
-
- func stopNotifier() {
- print("--- stop notifier")
- reachability?.stopNotifier()
- NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: nil)
- reachability = nil
- }
-
- func updateLabelColourWhenReachable(_ reachability: Reachability) {
- print("\(reachability.description) - \(reachability.connection)")
- if reachability.connection == .wifi {
- self.networkStatus.textColor = .green
- } else {
- self.networkStatus.textColor = .blue
- }
-
- self.networkStatus.text = "\(reachability.connection)"
- }
-
- func updateLabelColourWhenNotReachable(_ reachability: Reachability) {
- print("\(reachability.description) - \(reachability.connection)")
-
- self.networkStatus.textColor = .red
-
- self.networkStatus.text = "\(reachability.connection)"
- }
- @objc func reachabilityChanged(_ note: Notification) {
- let reachability = note.object as! Reachability
-
- if reachability.connection != .unavailable {
- updateLabelColourWhenReachable(reachability)
- } else {
- updateLabelColourWhenNotReachable(reachability)
- }
- }
-
- deinit {
- stopNotifier()
- }
- }
|