Search code examples
iosreachability

Detect online status at app launch / resume time - network might start up delayed?


I want to perform a reachability check each time my app comes to foreground in order to warn the user that remote data might be outdated. Can I rely on the fact, that 3G or Wi-Fi have been properly initialized at app start? Or should I delay the reachability check in order to avoid "false positives". If I should delay it, what would be a sufficient delay interval?

Maybe there is even a more recommended approach?


Solution

  • Always try to do your connection first. Only when it fails should you use Reachability to determine if you're offline, and to determine when you should automatically retry.

    Trying your connection may re-activate the network in a way that checking for it won't.

    It is, however, entirely appropriate to listen for Reachability's notification that the network has come online and to try again then (assuming you're not already trying).

    To be clear:

    • Accessing the network may wake it up even if Reachability says it isn't available, so just try.
    • After trying and failing, Reachability should tell you if the networking was available or not. (Don't trust it completely, but do use it to customize your error messages.)
    • Reachability will detect the network waking up for other reasons, so try again when you get Reachability's notification.
    • Don't lose sight of how mind-bogglingly complex mobile networking can be, and make as few assumptions as possible.