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 {
    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


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.

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.

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!

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.

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.

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.

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.

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.

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.

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.

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.

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.

Adding a meteor shower

25:55

REMAKING APPS

Adding a meteor shower

Large parts of Apple’s Weather app is about bringing little sparks of joy to an otherwise very serious, fact-driven experience, but none more so than the random little meteors that fly by on starry nights. They move so fast so you might be tempted to skip over them, but I think it’s definitely worth exploring and having some fun with!

Integrating with Shortcuts

8:17

ULTIMATE PORTFOLIO APP

Integrating with Shortcuts

UPDATED: Shortcuts let users access quick commands from our app elsewhere in the system, as well as chaining them to build complex commands, or even asking Siri to trigger one directly. In this article we’re going to add one to our app, and I think you’ll be amazed how little work it takes!

Customizing Toggle using ToggleStyle

9:28

INTERMEDIATE SWIFTUI

Customizing Toggle using ToggleStyle

Most of the time the built-in iOS controls are great, but sometimes you want something just a little different. In this article I’m going to walk you through how you can take complete control over the way toggle switches work in SwiftUI, providing custom rendering and interactions.

How do you stay up to date with changes in Swift?

1:59

INTERVIEW QUESTIONS

How do you stay up to date with changes in Swift?

This is particularly important in the world of Apple development because all their major operating systems change every year, Swift sees significant changes two or three times a year, and new devices are shipping regularly.

Simple SwiftUI, part 1: SimpleToDo and SimpleScores

1:45:22

LIVE STREAMS

Simple SwiftUI, part 1: SimpleToDo and SimpleScores

In this article we’re going to build two simple SwiftUI projects back to back, as part of a new initiative to create easily accessible sample projects for learners.

RegexCoach

1:10:04

LIVE STREAMS

RegexCoach

In this stream we’re going to build a SwiftUI app that lets the user experiment with regular expressions: searching for patterns, matching groups, and even generating Swift code.

Challenge 1: Converter

34:06

SOLUTIONS

Challenge 1: Converter

This early challenge day asks you to build a converter app that’s able to move between any two similar units, such as kilometers and miles. Let’s solve it now, then we’ll take it further, and then we’ll take it even further

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.