TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

< 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 {
    self.objectWillChange.send()
}

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
        print(data)
    }.resume()
}

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!

Challenges

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


Trees

31:55

DATA STRUCTURES

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.

Ultimate Portfolio App: Introduction

11:03

ULTIMATE PORTFOLIO APP

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.

Interview questions: Introduction

3:54

INTERVIEW QUESTIONS

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…

Understanding assertions

27:33

INTERMEDIATE SWIFT

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.

Understanding generics – part 1

20:01

INTERMEDIATE SWIFT

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.

Shadows and glows

19:50

SWIFTUI SPECIAL EFFECTS

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.

Creating a WaveView to draw smooth waveforms

32:08

CUSTOM SWIFTUI COMPONENTS

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.

Making the most of optionals

23:07

ADVANCED SWIFT

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.

Using memoization to speed up slow functions

36:18

HIGH-PERFORMANCE APPS

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 custom property wrapper using DynamicProperty

14:20

INTERMEDIATE SWIFTUI

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.

Transforming data with map()

42:32

FUNCTIONAL PROGRAMMING

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!

How to use phantom types in Swift

24:11

ADVANCED 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.

Functional programming in Swift: Introduction

6:52

FUNCTIONAL PROGRAMMING

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.

Checkpoint 5

7:48

SOLUTIONS

Checkpoint 5

Checkpoint 5 of Swift for Complete Beginners asks you to process an array of numbers using filter(), sorted(), and map(). Let’s solve that now…

Introducing concurrency

1:22:58

EVENTS

Introducing concurrency

The concurrency changes in Swift are powerful and wide-ranging, so if you want to take advantage of new SwiftUI features such as task() or refreshable() you need to start here.

Just for fun: weather forecasting

1:09:19

EVENTS

Just for fun: weather forecasting

Now you understand how widgets work, I want to give you a fun and simple task to experiment with. There’s no example solution here because this is just a sandbox for your ideas, but we’ll end up with a fun quiz to test what you learned.

Understanding generics – part 2

30:48

INTERMEDIATE SWIFT

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.

Phase animators and visual effects

1:28:40

EVENTS

Phase animators and visual effects

Before we start looking at the data changes this year, there are still more major SwiftUI changes to go over: phase animators and visual effects.

SumThing

1:06:27

LIVE STREAMS

SumThing

In the second of three streams about building games with SwiftUI, we’re going to create a mathematics puzzle game that asks user to add rows and columns to make targets – SwiftUI’s Grid really makes it easy!

What are the advantages and disadvantages of SwiftUI compared to UIKit?

2:27

INTERVIEW QUESTIONS

What are the advantages and disadvantages of SwiftUI compared to UIKit?

This is another example of a question where exclusively picking one over the other makes you look inexperienced – software development is often fuzzy, and acknowledging that complexity is a mark of experience rather than a weakness.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.