| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- //
- // 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?
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Start reachability without a hostname intially
- setupReachability(hostName: nil, useClosures: true)
- startNotifier()
- // After 5 seconds, stop and re-start reachability, this time using a hostname
- let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(UInt64(5) * NSEC_PER_SEC))
- dispatch_after(dispatchTime, dispatch_get_main_queue()) {
- self.stopNotifier()
- self.setupReachability(hostName: "google.com", useClosures: true)
- self.startNotifier()
- let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(UInt64(5) * NSEC_PER_SEC))
- dispatch_after(dispatchTime, dispatch_get_main_queue()) {
- self.stopNotifier()
- self.setupReachability(hostName: "invalidhost", useClosures: true)
- self.startNotifier() }
- }
- }
-
- func setupReachability(hostName hostName: String?, useClosures: Bool) {
- hostNameLabel.text = hostName != nil ? hostName : "No host name"
-
- print("--- set up with host name: \(hostNameLabel.text!)")
- do {
- let reachability = try hostName == nil ? Reachability.reachabilityForInternetConnection() : Reachability(hostname: hostName!)
- self.reachability = reachability
- } catch ReachabilityError.FailedToCreateWithAddress(let address) {
- networkStatus.textColor = UIColor.redColor()
- networkStatus.text = "Unable to create\nReachability with address:\n\(address)"
- return
- } catch {}
-
- if (useClosures) {
- reachability?.whenReachable = { reachability in
- dispatch_async(dispatch_get_main_queue()) {
- self.updateLabelColourWhenReachable(reachability)
- }
- }
- reachability?.whenUnreachable = { reachability in
- dispatch_async(dispatch_get_main_queue()) {
- self.updateLabelColourWhenNotReachable(reachability)
- }
- }
- } else {
- NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)
- }
- }
-
- func startNotifier() {
- print("--- start notifier")
- do {
- try reachability?.startNotifier()
- } catch {
- networkStatus.textColor = UIColor.redColor()
- networkStatus.text = "Unable to start\nnotifier"
- return
- }
- }
-
- func stopNotifier() {
- print("--- stop notifier")
- reachability?.stopNotifier()
- NSNotificationCenter.defaultCenter().removeObserver(self, name: ReachabilityChangedNotification, object: nil)
- reachability = nil
- }
-
- func updateLabelColourWhenReachable(reachability: Reachability) {
- print("\(reachability.description) - \(reachability.currentReachabilityString)")
- if reachability.isReachableViaWiFi() {
- self.networkStatus.textColor = UIColor.greenColor()
- } else {
- self.networkStatus.textColor = UIColor.blueColor()
- }
-
- self.networkStatus.text = reachability.currentReachabilityString
- }
- func updateLabelColourWhenNotReachable(reachability: Reachability) {
- print("\(reachability.description) - \(reachability.currentReachabilityString)")
- self.networkStatus.textColor = UIColor.redColor()
-
- self.networkStatus.text = reachability.currentReachabilityString
- }
-
- func reachabilityChanged(note: NSNotification) {
- let reachability = note.object as! Reachability
-
- if reachability.isReachable() {
- updateLabelColourWhenReachable(reachability)
- } else {
- updateLabelColourWhenNotReachable(reachability)
- }
- }
-
- deinit {
- stopNotifier()
- }
- }
|