Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine example #355

Open
mycroftcanner opened this issue Nov 19, 2019 · 0 comments
Open

Combine example #355

mycroftcanner opened this issue Nov 19, 2019 · 0 comments

Comments

@mycroftcanner
Copy link

mycroftcanner commented Nov 19, 2019

import Combine
import Reachability
import os

class ReachabilityStore: ObservableObject {
    private var reachability: Reachability

    @Published var reachable: Bool = false
    @Published var reachableViaWifi: Bool = false
    @Published var reachableViaCellular: Bool = false

    init() {
        reachability = try! Reachability()
        
        reachability.whenReachable = { [weak self] reachability in
            guard let self = self else { return }

            self.reachable = true
            self.reachableViaWifi = reachability.connection == .wifi
            self.reachableViaCellular = !self.reachableViaWifi

            os_log(
                "Reachable via %{public}s",
                self.reachableViaWifi ? "WiFi" : "Cellular"
            )
        }

        reachability.whenUnreachable = { [weak self] _ in
            guard let self = self else { return }

            self.reachable = false
            self.reachableViaWifi = false
            self.reachableViaCellular = false

            os_log("Unreachable")
        }

        do {
            try reachability.startNotifier()
        } catch {
            os_log("Unable to start reachability notifier.")
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant