FREE: Read a new Swift article every day – click here! >>

How to check for internet connectivity using NWPathMonitor

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.

 

  • Advent of Swiftmas Day Four: Save 50% on my book Swift Design Patterns, and learn to take advantage of Swift’s native techniques so you can build smarter, safer apps – find out more here.

     

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.")
    }

    print(path.isExpensive)
}

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 monitor = 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.

Available from iOS 12.0

Did this solution work for you? Please pass it on!

Other people are reading…

About the Swift Knowledge Base

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

Get my latest video for free

Learn about value types, functional programming, and protocol-oriented programming in this new video – it's free!

MASTER SWIFT NOW
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 me know!

Average rating: 3.8/5

Click here to visit the Hacking with Swift store >>