Skip to content

Instantly share code, notes, and snippets.

@Savchukv
Last active May 31, 2017 14:13
Show Gist options
  • Save Savchukv/792c4393275b6b167214c142e658263e to your computer and use it in GitHub Desktop.
Save Savchukv/792c4393275b6b167214c142e658263e to your computer and use it in GitHub Desktop.
Check internet connection
//
// Reachability.swift
//
// Created by Vasiliy Savchuk on 06.02.17.
// Copyright © 2017 All rights reserved.
//
// Usage this snippet:
// guard Reachability.sharedInstance.isReachable else {
// show alert
// return
// }
import Foundation
import Alamofire
class Reachability {
let networkReachabilityManager = NetworkReachabilityManager()
var isReachableWiFi: Bool = false
var isReachable: Bool = false {
didSet {
NotificationCenter.sendNotification(name:Notification.Name.Reachability.didChange)
}
}
//MARK: - Class Methods
static let sharedInstance : Reachability = {
let instance = Reachability()
instance.observeReachability()
instance.networkReachabilityManager?.startListening()
return instance
}()
//MARK: - Methods
func observeReachability() {
networkReachabilityManager?.listener = { status in
print("Network Status Changed: \(status)")
switch status {
case .notReachable:
self.isReachable = false
self.isReachableWiFi = false
break
case .reachable(NetworkReachabilityManager.ConnectionType.ethernetOrWiFi):
self.isReachable = true
self.isReachableWiFi = true
break
case .reachable(NetworkReachabilityManager.ConnectionType.wwan), .unknown:
self.isReachable = true
self.isReachableWiFi = false
break
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment