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

whenReachable() and whenUnreachable() do not work #144

Closed
tiana890 opened this issue Sep 19, 2016 · 3 comments
Closed

whenReachable() and whenUnreachable() do not work #144

tiana890 opened this issue Sep 19, 2016 · 3 comments

Comments

@tiana890
Copy link

tiana890 commented Sep 19, 2016

Hello. Can you help me with my problem? I have always used this framework in my apps but now I can't figure out what I am doing wrong.

In my viewDidLoad() function I call this method:

//MARK: Reachability
func startReachability(){
//declare this property where it won't go out of scope relative to your listener
do{
    let reachability = try Reachability.reachabilityForInternetConnection()

    reachability.whenReachable = { reachability in
        // this is called on a background thread, but UI updates must
        // be on the main thread, like this:
        dispatch_async(dispatch_get_main_queue()) {
            if reachability.isReachableViaWiFi() {
                print("Reachable via WiFi")
            } else {
                print("Reachable via Cellular")
            }
        }
    }
    reachability.whenUnreachable = { reachability in
        // this is called on a background thread, but UI updates must
        // be on the main thread, like this:
        dispatch_async(dispatch_get_main_queue()) {
            print("Not reachable")
        }
    }

    try reachability.startNotifier()

} catch {
    print("Unable to start notifier")
}
}

But it does not work. It calls whenReachable() and whenUnreachable() only once and when I turn Wi-Fi off and on it does nothing. I use 2.4 version.

@somepablo
Copy link

Same for me using iOS 10 and debugging on a real device.

@ashleymills
Copy link
Owner

You need a strong reference to your reachability variable, so declare it at class level. See the example in the readme, issues #93, #85

@tiana890
Copy link
Author

@ashleymills thank u very much) it is evident but sometimes shit happens)))

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

3 participants