Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leasual/daefe02d6eba3092199a5179d3cfa5a0 to your computer and use it in GitHub Desktop.
Save leasual/daefe02d6eba3092199a5179d3cfa5a0 to your computer and use it in GitHub Desktop.
import Foundation
import Reachability
//Reachability
//declare this property where it won't go out of scope relative to your listener
fileprivate var reachability: Reachability!
protocol ReachabilityActionDelegate {
func reachabilityChanged(_ isReachable: Bool)
}
protocol ReachabilityObserverDelegate: class, ReachabilityActionDelegate {
func addReachabilityObserver()
func removeReachabilityObserver()
}
// Declaring default implementation of adding/removing observer
extension ReachabilityObserverDelegate {
/** Subscribe on reachability changing */
func addReachabilityObserver() {
reachability = Reachability()
reachability.whenReachable = { [weak self] reachability in
self?.reachabilityChanged(true)
}
reachability.whenUnreachable = { [weak self] reachability in
self?.reachabilityChanged(false)
}
do {
try reachability.startNotifier()
} catch {
print("Unable to start notifier")
}
}
/** Unsubscribe */
func removeReachabilityObserver() {
reachability.stopNotifier()
reachability = nil
EventsManager.shared.removeListener(self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment