Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
}
}
@popcornomnom
Copy link
Author

popcornomnom commented Dec 12, 2019

@ surajkumarmandal thanks for the help, I have updated gist for Swift 5

@sarvesh829
Copy link

sarvesh829 commented Mar 29, 2022

How i can use this please comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment