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
func networkStatusDidChange(status: Reachability.NetworkStatus) { | |
switch status { | |
case .notReachable: | |
debugPrint("ViewController: Network became unreachable") | |
case .reachableViaWiFi: | |
debugPrint("ViewController: Network reachable through WiFi") | |
case .reachableViaWWAN: | |
debugPrint("ViewController: Network reachable through Cellular Data") | |
} |
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
extension ViewController: NetworkStatusListener { | |
func networkStatusDidChange(status: Reachability.NetworkStatus) { | |
switch status { | |
case .notReachable: | |
debugPrint("ViewController: Network became unreachable") | |
case .reachableViaWiFi: | |
debugPrint("ViewController: Network reachable through WiFi") | |
case .reachableViaWWAN: |
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
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
ReachabilityManager.shared.addListener(listener: self) | |
} | |
override func viewDidDisappear(_ animated: Bool) { | |
super.viewDidDisappear(animated) | |
ReachabilityManager.shared.removeListener(listener: self) | |
} |
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
// Start | |
ReachabilityManager.shared.startMonitoring() | |
// Stop | |
ReachabilityManager.shared.stopMonitoring() |
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
/// Called whenever there is a change in NetworkReachibility Status | |
/// | |
/// — parameter notification: Notification with the Reachability instance | |
func reachabilityChanged(notification: Notification) { | |
let reachability = notification.object as! Reachability | |
switch reachability.currentReachabilityStatus { | |
case .notReachable: | |
debugPrint("Network became unreachable") |
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
// Sending message to each of the listeners | |
for listener in listeners { | |
listener.networkStatusDidChange(status: reachability.currentReachabilityStatus) | |
} |
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
/// Adds a new listener to the listeners array | |
/// | |
/// - parameter delegate: a new listener | |
func addListener(listener: NetworkStatusListener){ | |
listeners.append(listener) | |
} | |
/// Removes a listener from listeners array | |
/// | |
/// - parameter delegate: the listener which is to be removed |
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
// 6. Array of delegates which are interested to listen to network status change | |
var listeners = [NetworkStatusListener]() |
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
/// Protocol for listenig network status change | |
public protocol NetworkStatusListener : class { | |
func networkStatusDidChange(status: Reachability.NetworkStatus) | |
} |