Beginning Networking with URLSession

Sep 13 2022 · Swift 5.6, iOS 15, Xcode 13.4.1

Part 1: Introduction to URLSession & Concurrency

05. Session Configurations

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 04. Challenge: Run Code on the Main Thread Next episode: 06. Get Data from a Session

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

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 the equivalent of privacy mode in browsers. That is, it doesn't cache anything or store credentials or any session related data to disc. This is managed by a URLSession configuration object. There are three types of this object, a default configuration, an ephemeral configuration, and a background configuration. A default configuration object uses a persistent disc 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 cache data to disc. 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 it 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, time interval for resource determines the time a resource should take. The wait for connectivity 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 are any non-constrained interfaces available and this property is set to false, all tasks will fail. Allows expensive network access determines whether this 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 waits for connectivity to true. This means that the task will wait for an available interface before starting. Let's try out URLSession configuration to get an idea of how it works. Create a new playground and remove all its contents. Add an import for foundation. The easiest way to get a URLSession configuration is to create a new URLSession. You can do that from the shared singleton session object. From here you can access the configuration object using a property. So if you wanna see the value of allow cellular access, you can access it as follows. Run your code. 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 allow cellular to false. Run your code. Nothing really changed. To create a configuration object, you access the default static property of your URLSession configuration. You can also do this for ephemeral configurations and a background configurations as well. Notice you have to give the background configuration and 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 allow cellular access property. Run your code. Excellent. If you want the connection to support expensive access, you can set the allows expensive network access property. You can also set the allows constrained network access property here as well. Create a new session using the my default configuration object, and you'll see that it's using the settings on it. Run your code. If you don't want to change any properties and instead want to use the default configuration, you can create a session as follows. And the value of allow cellular access is the default value true. Run the code one last time. Great work. Knowing how to work with URLSession configuration opens the door to working with URLSession itself which is the topic of the next episode. So I'll see you there.