UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

< Back to Latest Articles

User-friendly network access

Anyone can write Swift code to fetch network data, but much harder is knowing how to write code to do it respectfully. In this article we’ll look at building a considerate network stack, taking into account the user’s connection, preferences, and more.

Watch the video here, or read the article below

Quick links

Optimal network detection

Let’s start with the basics: do we even have an active network connection?

Years ago, before “reachability” was used to make it easier to reach distant parts of an iPhone screen, Apple used the name for a small network class that would detect whether a server could be reached or not. Over the years this has been forked multiple times, and you’ll still commonly see it used in the wild.

These days, though, there’s a much better solution called NWPathMonitor. It comes with the Network framework that was introduced in iOS 12, and we can either restrict it to using particular network types (e.g. WiFi or cellular) or just let it work across all interfaces.

To use it takes a few steps, so we’re going to create a simple wrapper for it so you can either monitor it directly with SwiftUI, or in other frameworks using Combine.

This takes five small steps in total, starting with creating a new class that conforms to the ObservableObject protocol so we can announce updates as they come in. We’ll need to add some properties to this, the most important two of which are one to store the internal NWPathMonitor watching for changes, and one to store a DispatchQueue where the monitoring work will take place.

So, start by adding an import for Network, then add this new class to the same file:

class NetworkMonitor: ObservableObject {
    private let monitor = NWPathMonitor()
    private let queue = DispatchQueue(label: "Monitor")

Second, to make this easier to use we’re going to add four properties that describe the network availability we have: whether it’s active (do we have access to the network?), whether it’s expensive (cellular or WiFi using a cellular hotspot), whether it’s constrained (restricted by low data mode), and the exact connection type we have (WiFi, cellular, etc).

So, add these properties to NetworkMonitor:

var isActive = false
var isExpensive = false
var isConstrained = false
var connectionType = NWInterface.InterfaceType.other

Third, we need to give the class an initializer that tells the internal NWPathMonitor what it should do when changes come in, and tells it start monitoring on our private queue:

init() {
    monitor.pathUpdateHandler = { path in
        // more code here

    monitor.start(queue: queue)

Now for the fractionally more complex part: whenever that // more code here closure is run, we need to store our latest networking values in our property, then notify anyone who is watching for updates.

This is mostly easy to do, but we do need to make an array of all possible connection types then find the first one that is actually available.

Replace the // more code here comment with this:

self.isActive = path.status == .satisfied
self.isExpensive = path.isExpensive
self.isConstrained = path.isConstrained

let connectionTypes: [NWInterface.InterfaceType] = [.cellular, .wifi, .wiredEthernet]
self.connectionType = connectionTypes.first(where: path.usesInterfaceType) ?? .other

DispatchQueue.main.async {

For the final step, we need to create an instance of NetworkMonitor then inject it into the SwiftUI environment. We can do that in the scene delegate by replacing the let contentView line with this:

let monitor = NetworkMonitor()

let contentView = ContentView().environmentObject(monitor)

And that’s it! We now have a reusable network monitor that works great in SwiftUI, but also can be used in UIKit and other frameworks because it just uses a simple Combine publisher.

To try it out in SwiftUI, use code like this:

struct ContentView: View {
    @EnvironmentObject var network: NetworkMonitor

    var body: some View {
        Text(verbatim: """
        Active: \(network.isActive)
        Expensive: \(network.isExpensive)
        Constrained: \(network.isConstrained)

Once it’s running, you’ll find your UI automatically updates if you toggle WiFi or flight mode.

Waiting for access

If you want really great network access then you need to create your own URLSession. This is easy to do, and although it gives us control over the same constrained and expensive checks as before, we now also gain the ability to control caching and waiting – it’s one of the most beautiful parts of Apple’s APIs, and I wish more folks used it!

Here’s how a basic configuration might look:

func makeRequest() {
    let config = URLSessionConfiguration.default
    config.allowsExpensiveNetworkAccess = false
    config.allowsConstrainedNetworkAccess = false

    let session = URLSession(configuration: config)
    let url = URL(string: "https://www.apple.com")!

    session.dataTask(with: url) { data, response, error in

With expensive and constrained network access disabled, that request will fail if Low Data Mode is enabled or if the user is on an expensive connection – i.e., cellular or WiFi backed by a cellular hotspot.

The nice thing about this approach is that it means all our requests honor our settings automatically – you can stash that session away somewhere, then make requests on it whenever you need.

But the real power is that we get new functionality, including the ability to defer our network request until access becomes available. This means we can start the request for some work now, provide it with whatever functionality we want to execute when it completes, and iOS will simply hold on to that until we finally have network access.

What does it take to make that happen? Just one Boolean:

config.waitsForConnectivity = true

To try it out, add that line to makeRequest(), then rewrite ContentView so it has a button that triggers makeRequest() when tapped:

var body: some View {
    Button("Fetch Data", action: makeRequest)

Try running that on a real device that has flight mode on and WiFi off – you’ll see nothing happens. But as soon as you enable some sort of network, you’ll see something like “Optional(67820 bytes)”, which is the completed download of the Apple website.

This is the ideal way of working with data, particularly when it’s large: we want to have it at some point but not necessarily now, and not if the user is using an expensive or constrained network.

But having a custom URL session allows us to go one step further: we can have complete control over the way data is cached. By default we’ll have our data while our app is running, but we can just adjust our configuration so that data is always reloaded like this:

config.requestCachePolicy = .reloadIgnoringLocalCacheData

Putting all this together, we can get sensible caching, requests that wait until network access is available, requests that refuse to work over expensive or constrained networks, and super slick network detection with NWPathMonitor – not bad!


There are a few tasks I recommend you try to make our code more flexible:

  1. Update the NetworkMonitor class so that we can create the internal NWPathMonitor using a specific interface type if needed. This should be alongside the current initializer, but it should allow us to request WiFi specifically.
  2. Also add a way to stop our network monitor from working.

If you liked this, you'd love Hacking with Swift+…

Here's just a sample of the other tutorials, with each one coming as an article to read and as a 4K Ultra HD video.

Find out more and subscribe here

Creating a custom property wrapper using DynamicProperty



FREE: Creating a custom property wrapper using DynamicProperty

It’s not hard to make a basic property wrapper, but if you want one that automatically updates the body property like @State you need to do some extra work. In this article I’ll show you exactly how it’s done, as we build a property wrapper capable of reading and writing documents from our app’s container.




FREE: Trees

Trees are an extraordinarily simple, extraordinarily useful data type, and in this article we’ll make a complete tree data type using Swift in just a few minutes. But rather than just stop there, we’re going to do something quite beautiful that I hope will blow your mind while teaching you something useful.

Making the most of optionals



FREE: Making the most of optionals

Swift’s optionals are implemented as simple enums, with just a little compiler magic sprinkled around as syntactic sugar. However, they do much more than people realize, and in this article I’m going to demonstrate some of their power features that can really help you write better code – and blow your mind along the way.

Transforming data with map()



FREE: Transforming data with map()

In this article we’re going to look at the map() function, which transforms one thing into another thing. Along the way we’ll also be exploring some core concepts of functional programming, so if you read no other articles in this course at least read this one!

Understanding generics – part 1



FREE: Understanding generics – part 1

Generics are one of the most powerful features of Swift, allowing us to write code once and reuse it in many ways. In this article we’ll explore how they work, why adding constraints actually helps us write more code, and how generics help solve one of the biggest problems in Swift.

Understanding assertions



FREE: Understanding assertions

Assertions allow us to have Swift silently check the state of our program at runtime, but if you want to get them right you need to understand some intricacies. In this article I’ll walk you through the five ways we can make assertions in Swift, and provide clear advice on which to use and when.

Interview questions: Introduction



FREE: Interview questions: Introduction

Getting ready for a job interview is tough work, so I’ve prepared a whole bunch of common questions and answers to help give you a jump start. But before you get into them, let me explain the plan in more detail…

Using memoization to speed up slow functions



FREE: Using memoization to speed up slow functions

In this article you’ll learn how memoization can dramatically boost the performance of slow functions, and how easy Swift makes it thanks to its generics and closures.

Creating a WaveView to draw smooth waveforms



FREE: Creating a WaveView to draw smooth waveforms

In this article I’m going to walk you through building a WaveView with SwiftUI, allowing us to create beautiful waveform-like effects to bring your user interface to life.

Shadows and glows



FREE: Shadows and glows

SwiftUI gives us a modifier to make simple shadows, but if you want something more advanced such as inner shadows or glows, you need to do extra work. In this article I’ll show you how to get both those effects and more in a customizable, flexible way.

Functional programming in Swift: Introduction



FREE: Functional programming in Swift: Introduction

Before you dive in to the first article in this course, I want to give you a brief overview of our goals, how the content is structured, as well as a rough idea of what you can expect to find.

Ultimate Portfolio App: Introduction



FREE: Ultimate Portfolio App: Introduction

UPDATED: While I’m sure you’re keen to get started programming immediately, please give me a few minutes to outline the goals of this course and explain why it’s different from other courses I’ve written.

How to use phantom types in Swift



FREE: How to use phantom types in Swift

Phantom types are a powerful way to give the Swift compiler extra information about our code so that it can stop us from making mistakes. In this article I’m going to explain how they work and why you’d want them, as well as providing lots of hands-on examples you can try.

Can you talk me through some interesting code you wrote recently?



Can you talk me through some interesting code you wrote recently?

If I could make one question required in all iOS developer job interviews, it would be this one, because it’s a fantastic way to help folks relax, and lets them cherry pick whatever code they think will be most of interest.

Rendering a pie chart



Rendering a pie chart

Pie charts are a classic way of showing divided data visually, and they represent interesting challenges around sizing and angles. In this article we’ll build a complete pie chart view from scratch using SwiftUI, ensuring it works using animation, and also modify it to support donut-style charts too.

Advanced button customization using ButtonStyle



Advanced button customization using ButtonStyle

Previously we looked at how to create basic button styles that unify your app’s styling efficiently. In this follow-on article we’re going to explore three completely different button styles that show off just what SwiftUI is capable of: glossy marble buttons, classic fantasy buttons, and sci-fi buttons.

Understanding generics – part 2



Understanding generics – part 2

In this second tutorial on generics, we’re going to explore creating several different generic types, look at extending generics, and look at how we can apply our generics knowledge to create property wrappers.

What open source projects have you contributed to?



What open source projects have you contributed to?

This question comes up a lot, but I’m always wary of it because it places a lot of emphasis on unpaid work out of hours, which particularly affects people who already have extra responsibilities caring for children or elders.

What does the @Published property wrapper do?



What does the @Published property wrapper do?

A great answer should introduce property wrappers, then explain what @Published does, give some examples, compare it against @State, then bring in any extra real-world knowledge you can add around nuances or complexities.

Identifying performance hot spots with Time Profiler



Identifying performance hot spots with Time Profiler

Instruments gives us a range of tools for finding performance problems, and in this article we’ll be looking at how the Time Profiler instrument can point out problems in seconds.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.