At long last, we've arrived at the URL session. But before we can create a session, we need an instance of a URL session configuration. This lets you control things such as the network service type, timeouts, caching, and HTTP headers.
The default configuration object uses a persistent disk-based cache except when the result is downloaded to a file. It stores the credentials in the user's keychain. It uses default values for its properties unless you customize it. By default, we use the aptly named default configuration, but there is another type of configuration called an ephemeral configuration. An ephemeral configuration is like default, but it doesn't store cookies, credentials, or cache data to disk.
You can think of it like creating an incognito window in Chrome or going into privacy mode in Firefox. The last type of configuration is a background configuration. It can transfer data while the app runs in the background. It hands control of transfers to the system, which handles the transfers in a separate process. You must provide an identifier so that the system can reconstruct its sessions if the system terminates and relaunches your app.
When you create a configuration object, you can change any of its properties from the default values. However, you must make those changes before creating the URL session instance. Changes made to a configuration object after the session has been created have no effect on existing sessions. Now, configurations come with lots of properties.
For instance, you can determine whether you want the session to work on just Wi-Fi or have cellular access. You can set timeouts for requests, wait for when connectivity is available, or whether the session can accept cookies. There are a ton of different options, so when you have a moment, check out the NSURL configuration session NSL. URL session configuration documentation. Here I have a Playground file open and we're going to walk through this together to get an idea of how session configuration works.
So we'll start with the top. The easiest way to get a session configuration is to create a new session and you could do that from the shared singleton session object. From here, I just access the configuration object from a session. So if I want to see the value of allowCellularAccess, I can access it like so.
The configuration is already attached to the session, so the properties are now read-only. I need to configure my configuration before I add it to the session. So let's set the allowCellular property to false.
And you'll see that nothing really changed. To create a configuration object, we access the static property on URLSessionConfiguration. I can also do this for the ephemeral configurations and background configurations as well. Notice that we have to give the background configuration an identifier. This identifies the session.
If the app is terminated while the downloads are occurring, you can use this identifier to recreate the configuration and session objects associated with the transfer. Now notice that we change the allow cellular property. We'll also set the weights for connectivity to true. Apple recommends you do this for all non-background configurations.
Now I can create a new session with my default configuration, and you'll see that it is using my settings. If you don't want to change any properties and use the default configuration, you can instance a session like so. And now the value of allow cellular access is the default value of true.