NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Network Status Check

Forums > Swift

Hey guy. I am having an issue with that function that I found on hackingwithswift.com

 func checkNetworkStatus() {
        let monitor = NWPathMonitor()

        monitor.pathUpdateHandler = { path in
            if path.status == .satisfied {
                print("internet")
            }
            else {
                print("no internet")
            }
        }
        let queue = DispatchQueue(label: "Monitor")
        monitor.start(queue: queue)

    }

when I turn the wifi off, it always return "internet" Then when I turn it on, it prints "no internet" and then a second after "internet".

What am I doing wrong? Or is there any better way to check the internet connection? I don't necessarily need to check it everytime the network status changes. Only when some button is tapped. Many thanks.

1      

Hi, well this is quite a complicated topic for a few reasons.

The behavior looks like you have Cellular data, and when you switch back to WiFi there is a small window when the internet does not work because it is switching connections...

As for checking if there is connection before doing something, the problem is that there might be but then disappear once you start the main work. I think there is even mention in the official Apple guidelines to not check for connection before doing other network related tasks. I think better idea is to just try it and report to user possible problems.

Imagine you implement checking network connection against some popular DNS, but maybe DNS will go down and user cannot continue, even though they have connection and your API is also working fine.

URLSession also has settings where it waits for connectivity for specified amount of time and does not fail straight away.

If you really want some checking, I would keep using the NWPathMonitor and when it indicates that connection is not available, then maybe show warning banner in your UI. Something like: "Looks like there might be a connection issue". But dont disable the button

1      

Hey Filip!

I am currently using Firebase in my project. The issue is (altough it's a good thing if you think about it) that Firebase is smart enough to not return an error when there is no connection but to wait with the execution until the connection appears. Therefore I cant return any error. While potential user may search through something that already does not exist in a firebase while the network is off...

1      

Use this instead, it will fix your issue...

if path.status != .unsatisfied { print("internet") }

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.

Learn more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.