URLSession: The Core of iOS Networking

URLSession is the engine for all network requests in Apple apps. Use it to fetch API data, download images, or upload files. The biggest footgun is blocking the main UI thread, which freezes your app; always perform network tasks asynchronously.
Why it exists
Apps need to talk to the internet. Apple created URLSession to provide a single, powerful framework for all networking, replacing older, more complex APIs and reducing the need for third-party libraries for common tasks like fetching data or downloading files.
The mental model
Think of URLSession as your app's dedicated networking department. You give it a URLRequest (a memo describing what you want), and it handles the entire process: making the connection, sending the request, and returning with the data, an error, or a file on disk. It manages all the low-level complexity for you.
How it works
You typically use the shared singleton, URLSession.shared, for basic requests. You create a task, like a dataTask for fetching data or a downloadTask for files, by providing a URL. The key is that you must call resume() on the task to start it. The work is done asynchronously, and when it's finished, a completion handler you provided is called with the results. Modern Swift with async/await makes this even cleaner, letting you await the result of URLSession.shared.data(from: url).
When to use it
Use it for virtually any network communication in an iOS, macOS, watchOS, or tvOS app. It's the correct tool for fetching JSON from a REST API, downloading large assets for offline use, and uploading user-generated content to a server.
When not to use it
There's almost no reason to avoid it. While a third-party library might offer syntactic sugar, URLSession's native integration and modern Swift syntax make it powerful and easy to use. You might not interact with it directly if you use a higher-level SDK (like Firebase or a GraphQL client) that manages networking for you, but it's still running URLSession under the hood.
One canonical example
To fetch user data from an API, you first create a URL object for the endpoint. Then, using async/await, you can write let (data, response) = try await URLSession.shared.data(from: url). You then check the response's status code and use JSONDecoder to parse the received data into your Swift User model. This is the modern, standard pattern.
Interview question
What is the primary reason URLSession tasks should always be performed asynchronously in an iOS app?
- a.To ensure that all network communications are encrypted by default.
- b.To reduce the overall data usage and optimize network bandwidth.
- c.To allow the app to process multiple network requests simultaneously without conflicts.
- d.To prevent the app's user interface from becoming unresponsive or freezing.Correct
Why? this is the answer
The card explicitly states that "The biggest footgun is blocking the main UI thread, which freezes your app; always perform network tasks asynchronously." This directly addresses the need to keep the UI responsive. While asynchronous operations enable concurrency (Option C), the critical reason for their mandatory use in UI apps is to avoid blocking the main thread.
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