Forked from popcornomnom/ReachabilityObserverDelegate.swift
Created
June 21, 2019 02:36
-
-
Save leasual/daefe02d6eba3092199a5179d3cfa5a0 to your computer and use it in GitHub Desktop.
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
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