ashleymills / Reachability.swift

Replacement for Apple's Reachability re-written in Swift with closures
MIT License
7.94k stars 948 forks source link

notifier is called only once! #357

Closed MegaMaziar closed 4 years ago

MegaMaziar commented 4 years ago

I test this on a simple single-page iOS app and see the print-out when I turn wifi on and see the print out again when I turn it off, but that's it. On subsequent turn off/on I don't see any further print outs. Here is my entire code: (Should I not see message prints each time the status of connectivity changes?)

Code:

import UIKit import Reachability

let reachability = try! Reachability()

class ViewController: UIViewController {

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    reachability.whenReachable = { reachability in
        if reachability.connection == .wifi {
            print("WiFi connected")
        } else {
            print("Cellular connected")
        }
    }

    reachability.whenUnreachable = { _ in
        print("Not reachable")
    }

    do {
        try reachability.startNotifier()
    } catch {
        print("Unable to start notifier")
    }
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    reachability.stopNotifier()
}

}

output:

WiFi connected Not reachable

MegaMaziar commented 4 years ago

This happens only on simulator. On a physical device, it works fine and as expected. Hence closing the issue.