Leave a rating/review
Notes: 05. Session Configurations
Welcome back! The time has come to properly learn about URLSession. URLSession is a collection of related tasks. With URLSession, you can configure it per your app’s needs.
For instance, you could configure all your tasks to run in the background, or you could also configure your tasks to run in the equivalent of privacy mode in browsers; that is, it doesn’t cache anything, or store credentials or any session related data to disk.
This is managed by a URLSessionConfiguration object. There are three types of this object: 1. A default configuration 2. An ephemeral configuration 3. And background configuration.
A default configuration object uses a persistent disk-based cache, except when the result is downloaded to a file. It stores credentials in the user’s keychain. And It uses default values for its properties, unless you customize it.
An ephemeral configuration is like default, but it doesn’t store cookies, credentials, or cached data to disk. You can think of it like creating a private window in Safari or your preferred browser.
The last configuration type is a background configuration. It can transfer data while the app runs in the background. It also hands control of transfers over to the system, which handles the transfers in a separate process.
You must provide an identifier so the system can reconstruct its sessions in case the operating system terminates and relaunches your app. When you create a configuration object, you can change any of its properties from their default values.
However, you must make those changes before creating the URLSession instance. Changes made to a configuration object after the session has been created have no effect on existing sessions.
Configurations have several properties to determine how the app should access the network. For example, the timeIntervalForResource property determines the time a resource should take.
The waitsForConnectivity property indicates whether a session should wait for connectivity or fail immediately.
You use the configuration set cookie policies, cache policies, and minimum and maximum support for the TLS, that is Transport Layer Security protocol.
You can also use the configuration to set the timeout interval, or whether the connection should use cellular access, which is true by default.
iOS also gives us a few other features. First, you can designate a connection to allow constrained network access. If a user puts their device in Low Data Mode, then their connection is thereby constrained.
If there aren’t any non-constrained interfaces available, and this property is set to false, all tasks will fail.
allowsExpensiveNetworkAccess determines whether the session should use a limited network connection like a cellular connection or personal hotspot.
Like the constrained network property, if this is set to false and there are no non-expensive network interfaces, all tasks will fail.
For both the constrained access and expensive access properties you can put off tasks for a later time by setting waitsForConnectivity to true. This means that the task will wait for an available interface before starting.
Let’s try out URLSessionConfiguration to get an idea of how it works. Create a new playground, and remove all it’s contents. Add an import for Foundation:
import Foundation
The easiest way to get a URLSessionConfiguration is to create a new URLSession. You can do that from the shared singleton session object:
let sharedSession = URLSession.shared
From here, you can access the configuration object using a property. So if you want to see the value of allowsCellularAccess, you can access it as follows:
sharedSession.configuration.allowsCellularAccess
The configuration is already attached to the session so the properties are read-only. You need to set up your configuration before adding it to the session. Let’s see what happens when you set allowsCellular to false:
sharedSession.configuration.allowsCellularAccess = false
sharedSession.configuration.allowsCellularAccess
Nothing really changed. :/
To create a configuration object, you access the default static property of URLSessionConfiguration:
let myDefaultConfiguration = URLSessionConfiguration.default
You can also do this for ephemeral configurations and background configurations as well:
let ephemeralConfiguration = URLSessionConfiguration.ephemeral
let backgroundConfiguration = URLSessionConfiguration
.background(withIdentifier: "com.learningAbout.URLSession")
Notice you have to give the background configuration an identifier. This identifies the session. If the app is terminated while downloads are occurring you can use the identifier to recreate the configuration and session objects associated with the transfer. * And look at what happens if you change the allowsCellularaccess property:
myDefaultConfiguration.allowsCellularAccess = false
myDefaultConfiguration.allowsCellularAccess
Excellent! If you want the connection to support expensive access, you can set the `allowsExpensiveNetworkAccess’ property:
myDefaultConfiguration.allowsExpensiveNetworkAccess = true
You can also set the allowsConstrainedNetworkAccess property here as well:
myDefaultConfiguration.allowsConstrainedNetworkAccess = true
Create a new session using the myDefaultConfiguration object and you’ll see that it’s using the settings on it:
let myDefaultSession = URLSession(configuration: myDefaultConfiguration)
myDefaultSession.configuration.allowsConstrainedNetworkAccess
If you don’t want to change any properties and instead want to use the default configuration, you can create a session as follows:
let defaultSession = URLSession(configuration: .default)
And the value of allowsCellularAccess is the default value true:
defaultSession.configuration.allowsCellularAccess
Great work! Knowing how to work with URLSessionConfiguration opens the door to working with URLSession itself, which is the topic of the next episode. So I’ll see ya there! :)