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


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.

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.

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.

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.

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.

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.

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.

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…

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.

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.

Final tips then SwiftUI

1:12:54

EVENTS

Final tips then SwiftUI

We’re about to start exploring the many SwiftUI changes introduced this year, but before we’re done with concurrency we have two last techniques to explore.

Binary trees

33:11

DATA STRUCTURES

Binary trees

We already looked at trees, where each node can have zero or more children, and now I want to look at a specialized version called binary trees, where each node has zero, one, or two children. In particular we’re looking to look at how these lead to binary search trees and the remarkable performance advantages they can bring.

Customizing ProgressView using ProgressViewStyle

15:06

INTERMEDIATE SWIFTUI

Customizing ProgressView using ProgressViewStyle

SwiftUI’s ProgressView gives us control over showing determinate or indeterminate progress, but it’s a bit dull – just a thin line and an activity spinner. Fortunately, we also get the ProgressViewStyle protocol so we can build entirely custom progress views, and in this article I’ll show you how it’s done.

Creating a StretchingHeader that sticks to the top of a view

18:35

CUSTOM SWIFTUI COMPONENTS

Creating a StretchingHeader that sticks to the top of a view

When users scroll beyond the top of a scroll view the default behavior is to show some empty space, but many apps prefer to show a stretchy header area instead. In this article I’ll show you how to build that SwiftUI, making an image that stays fixed to the top no matter what.

Ordered sets

22:31

DATA STRUCTURES

Ordered sets

Arrays and sets have their own advantages and disadvantages, but what if we could combine them both to make an ordered set? We can! And in this article that’s exactly what we’ll do.

Creating a LongPressButton to trigger after delay

16:02

CUSTOM SWIFTUI COMPONENTS

Creating a LongPressButton to trigger after delay

In this article I’m going to walk you through building a LongPressButton with SwiftUI, which will requires users to press and hold for a second before it’s triggered.

Controlling views using the accelerometer

39:03

SWIFTUI SPECIAL EFFECTS

Controlling views using the accelerometer

Reading device motion and orientation is a fast and slightly magical way to incorporate the real world into your apps, and can do a huge amount to add a little spark of delight to your UI. In this article I’m going to show you how easy it is to control SwiftUI layouts using the accelerometer, and give you a few ideas for special effects.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.