Forked from popcornomnom/ReachabilityObserverDelegate.swift
Created
February 7, 2021 16:14
-
-
Save sotheavuthnguon/0b0c67f96cc50e99adac5d4c23a53369 to your computer and use it in GitHub Desktop.
This file contains hidden or 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() throws | |
| func removeReachabilityObserver() | |
| } | |
| // Declaring default implementation of adding/removing observer | |
| extension ReachabilityObserverDelegate { | |
| /** Subscribe on reachability changing */ | |
| func addReachabilityObserver() throws { | |
| reachability = try Reachability() | |
| reachability.whenReachable = { [weak self] reachability in | |
| self?.reachabilityChanged(true) | |
| } | |
| reachability.whenUnreachable = { [weak self] reachability in | |
| self?.reachabilityChanged(false) | |
| } | |
| try reachability.startNotifier() | |
| } | |
| /** Unsubscribe */ | |
| func removeReachabilityObserver() { | |
| reachability.stopNotifier() | |
| reachability = nil | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment