-1

I have Implemented internet check in my code which is working perfectly fine, but There is an issue ! Let say if Device is connected to a wifi BUT internet is not working, limited access or internet is down from some reason.. How to identify this any suggestions any best practice ???

Ahsan
  • 503
  • 4
  • 12

1 Answers1

0

Yes, You can do it using NWPathMonitor. for implementation, you need to follow the following steps.

Step 1:

import Network

Step 2:

create an instance of NWPathMonitor somewhere it won’t get freed immediately. For example, you might have it as a property on a view controller, for example:

let monitor = NWPathMonitor()

Step 3:

Now assign a closure to that monitor that will be triggered whenever network accessibility changes. This needs to accept one parameter, which is an NWPath describing the network access that is currently possible.

monitor.pathUpdateHandler = { path in
    if path.status == .satisfied {
        print("We're connected!")
    } else {
        print("No connection.")
    }

    print(path.isExpensive)
}

Source: https://www.hackingwithswift.com/example-code/networking/how-to-check-for-internet-connectivity-using-nwpathmonitor

Jarvis The Avenger
  • 2,750
  • 1
  • 19
  • 37