Checking for internet connection

How do I check to see if the user is connected to the internet, and if not, display a notification? I know how to display the notification just not how to trigger it if the connection is offline.

Accepted Reply

In short:

  1. Set

    waitsForConnectivity
    on the configuration you use to create your
    NSURLSession
    .
  2. Start your request at any time.

  3. If there’s connectivity, the request will start immediately. If not, you’ll get a

    -URLSession:taskIsWaitingForConnectivity:
    delegate callback and you can adjust your UI accordingly.

For more details, watch WWDC 2017 Session 709 Advances in Networking, Part 2, where we discuss this feature in some detail.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

The problem here is one of definition. What does “connected to the internet” really mean? If the user is connected to the Internet in general but can’t reach the specific server you’re talking to, what do you want to do?

My general advice on this front is:

  • Don’t preflight network connections. Let the user try whatever they want to try and then handle any errors you get.

    IMPORTANT You have to write error handling code anyway — the user could lose connectivity just as you make the request — so you might as well lean on that as much as possible.

  • You can use the

    SCNetworkReachability
    API to guide your retry policy, to help interpret errors, and to display a UI indicating that the network is offline.

    Note This API is not particularly easy to call from Swift but you’ll find lots of folks have published various wrappers for it.

  • On iOS 11 and later you can use the new

    waitsForConnectivity
    flag to issue a request that will delay if the network is not available.

For more background on this issue see the Design for Variable Network Interface Availability section of the Networking Overview.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

What I'm trying to do if I cant connect to Firebase, To send an alert saying failed.

Please correct me if I’m wrong but I believe that Firebase is an HTTP[S]-based service. Thus, you don’t really connect to it, but rather you send requests to it and get responses. The only way to know for sure whether any given request will work is to try it, and that’s what I recommend you do.

As I mentioned before, no amount of preflighting your requests will guarantee that a request is going to work, thus you have to write code that handles errors properly. And given that you’re handling errors properly, you can just skip the preflighting bit (-:

If you want to display a UI that tells the user that the network seems to be offline and thus they’re likely to encounter problems, there’s two ways to approach this:

  • Use

    waitsForConnectivity
    . If the network is offline you’ll get a
    -URLSession:taskIsWaitingForConnectivity:
    delegate callback and you can use that to trigger your UI.
  • On older systems you can use reachability to:

    • Run your UI

    • Trigger retries

    However, when using reachability it’s important to try each request regardless of what reachability says. Reachability is subject to both false positives and false negatives.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

How do I use that, is it an if statement?

How do I use that, is it an if statement?

My post covered a bunch of different topics, so I don’t know what you mean by “that”. Please clarify.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

How do I call the waitsFOrConnection. Is it a matter of an if statement

In short:

  1. Set

    waitsForConnectivity
    on the configuration you use to create your
    NSURLSession
    .
  2. Start your request at any time.

  3. If there’s connectivity, the request will start immediately. If not, you’ll get a

    -URLSession:taskIsWaitingForConnectivity:
    delegate callback and you can adjust your UI accordingly.

For more details, watch WWDC 2017 Session 709 Advances in Networking, Part 2, where we discuss this feature in some detail.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"