NEW: Join my free 100 Days of SwiftUI challenge today! >>

How to check for internet connectivity using NWPathMonitor

Swift version: 5.1

Paul Hudson    @twostraws   

Apple’s Network framework provides a number of useful classes for working with network data, including one specifically designed to monitor network accessibility: NWPathMonitor. If you ever used Apple’s older Reachability system, NWPathMonitor replaces it fully.

To get started, first add an import for the Network framework:

import Network

Next, 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()

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.

NWPath has a few properties, but there are two in particular you’re likely to care about: status describes whether the connection is currently available or not, and isExpensive is set to true when using cellular data or when using WiFi that is hotspot routed through an iPhone’s cellular connection.

To try this out, here’s some code that prints a message when the user’s connection status changes, and also prints whether the connection is considered expensive or not:

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


Remember, that closure gets called every time the connection status changes.

Once your path monitor is created and configured, the final step is to create a custom DispatchQueue instance for the monitor to run, then call its start() method:

let queue = DispatchQueue(label: "Monitor")
monitor.start(queue: queue)

Once that’s done, your closure will get called every time the connection status changes, so you can add code there to update the rest of your app with the current connection status.

If you want more fine-grained control over the network check, you can create your NWPathMonitor using a specific interface type. For example, if you specifically wanted to check for cellular data and only cellular data, you would write this:

let cellMonitor = NWPathMonitor(requiredInterfaceType: .cellular)

You can also use .wifi or even wiredEthernet if you want. Omitting the interface type causes them all to be watched at the same time, which is probably what you’ll want most of the time.

LEARN SWIFTUI FOR FREE I have a massive, free SwiftUI video collection on YouTube teaching you how to build complete apps with SwiftUI – check it out!

Available from iOS 12.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Buy Testing Swift Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.6/5