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
}
}
@NerfBro

This comment has been minimized.

Copy link

NerfBro commented Oct 5, 2019

What does "EventsManager.shared.removeListener(self)" do ?

@popcornomnom

This comment has been minimized.

Copy link
Owner Author

popcornomnom commented Oct 5, 2019

@NerfBro, oops, this shouldn't be here :)
I updated the gist

@rustam-773

This comment has been minimized.

Copy link

rustam-773 commented Oct 17, 2019

can you show how to do this with more details?

@popcornomnom

This comment has been minimized.

Copy link
Owner Author

popcornomnom commented Oct 18, 2019

@rustam-773, you can see how to use the code above here. ☺️
ReachabilityHandler observes for Internet connection changes after calling addReachabilityObserver()

Details article

@surajkumarmandal

This comment has been minimized.

Copy link

surajkumarmandal commented Dec 12, 2019

reachability = Reachability() in line number 23 showing error "Call can throw, but it is not marked with 'try' and the error is not handled" in swift 5.

@popcornomnom

This comment has been minimized.

Copy link
Owner Author

popcornomnom commented Dec 12, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.