Last active
May 31, 2017 14:13
-
-
Save Savchukv/792c4393275b6b167214c142e658263e to your computer and use it in GitHub Desktop.
Check internet connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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