URLSessionConfiguration: The Blueprint for Network Requests

Think of URLSessionConfiguration as a blueprint for your network sessions. It defines rules like timeouts and caching policies upfront. Use it to create customized sessions, but remember you can't change the configuration after the session is created.
Why it exists
URLSessionConfiguration exists to separate the behavior of a networking stack from the execution of individual requests. Instead of configuring every request with timeouts, cache policies, and headers, you create a reusable template that defines these policies for an entire group of related requests managed by a URLSession.
The mental model
Think of URLSessionConfiguration as a blueprint for a URLSession. It's a set of instructions you create once, defining rules like timeouts, caching strategies, and custom headers. The URLSession is then built from this blueprint, and all its subsequent network tasks will automatically follow those rules, just like a construction crew follows a building plan.
How it works
You create an instance of URLSessionConfiguration using one of its three types. The .default configuration is a standard setup that uses a disk-based cache. The .ephemeral configuration creates a private session that doesn't save any data like caches, cookies, or credentials to disk. The .background configuration is for handling uploads or downloads when the app is not in the foreground.
After choosing a type, you customize its properties. For example, you can set timeoutIntervalForRequest to control how long to wait for a response, or add custom headers for all requests using httpAdditionalHeaders.
Finally, you pass this configuration object into the URLSession initializer. It's crucial to know that the URLSession makes a deep copy of the configuration. Any changes you make to your original configuration object after the session has been created will have no effect on that session.
When to use it
Use .default for most standard networking tasks where you want sensible caching behavior. Use .ephemeral for features like a private browsing mode or for any requests where you want to ensure no trace of the session is left on disk. Use .background for large file transfers that need to continue reliably even if the user switches to another app.
When not to use it
Do not try to configure the URLSession.shared singleton. It has a fixed, basic configuration and is not customizable. It's fine for very simple, one-off requests, but for anything requiring custom behavior—like specific timeouts or cache policies—you must create your own URLSession with a custom URLSessionConfiguration.
One canonical example
To create a session that times out requests after 10 seconds and adds a custom authorization header to every request, you would write: let config = URLSessionConfiguration.default config.timeoutIntervalForRequest = 10
config.httpAdditionalHeaders = ["Authorization": "Bearer YOUR_TOKEN"]
let session = URLSession(configuration: config)Now, any data task created with this session will inherit these settings.
Interview question
Which statement accurately describes the relationship between a URLSessionConfiguration object and the URLSession it configures?
- a.The URLSession.shared singleton can be reconfigured by modifying its default URLSessionConfiguration.
- b.The URLSession maintains a live reference to the configuration, allowing dynamic updates to its properties.
- c.Once a URLSession is created, subsequent changes to the original URLSessionConfiguration object will not alter the session's behavior.Correct
- d.The configuration object can be modified after session creation, but only for background tasks.
Why? this is the answer
The card states that URLSession makes a deep copy of the configuration, so any changes to the original configuration object after the session is created will have no effect on that session. Option B is incorrect because the session does not maintain a live reference for dynamic updates.
Just read this? Test yourself on what you have been reading.
Read the original → developer.apple.com
- #ios
- #swift
- #networking
- #urlsession
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.
See open roles