CHAPTER 18
Beginner
Networking and API Calls in Swift
Updated: May 16, 2026
7 min read
# CHAPTER 18
Networking and API Calls in Swift
1. Introduction
A mobile app without the internet is just a calculator. Every major app—Instagram, Spotify, Uber—functions by downloading data from external servers (APIs) and displaying it on the screen. In iOS, the engine responsible for sending HTTP requests to the internet is called URLSession. In this chapter, we will master Networking and API Calls in Swift. We will learn how to construct valid URLs, execute asynchronousGET requests using modern async/await syntax, and handle the raw binary data returned by RESTful APIs.
2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the concept of REST APIs and JSON endpoints.
-
Construct safe
URLobjects in Swift.
-
Understand the necessity of Asynchronous programming (
async/await).
-
Use
URLSession.shared.data(from:)to fetch internet data.
-
Handle networking errors gracefully using
do-catch.
3. What is an API?
An API (Application Programming Interface) is a messenger. Your iPhone asks the API, *"What is the weather in New York?"* The server processes the request and sends back raw text data (usually formatted as JSON) containing the temperature. *Example Endpoint:*https://jsonplaceholder.typicode.com/users (A free, public API that returns dummy user data).
4. Asynchronous Programming (async/await)
If you ask the internet for data, it might take 3 seconds to arrive. If your app stops and waits for those 3 seconds, the entire UI will freeze, and the OS will crash the app for being unresponsive.
We must tell Swift: *"Go fetch this data in the background. I am going to pause this specific function (await), but keep the rest of the app running. Wake this function back up when the data arrives."*
Functions that perform background tasks must be marked with the async keyword.
5. Constructing the Network Request
Let's build a function in our ViewModel to download data from a URL.
swift
6. Executing the Request from SwiftUI
We cannot just call a backgroundasync function inside a standard UI button. We must wrap the call in a Task block. A Task creates a safe, isolated background bubble for the asynchronous work to happen.
swift
7. Main Actor (Thread Safety)
CRITICAL RULE: UI changes can ONLY happen on the "Main Thread". If your background networkTask finishes, and you try to update a @Published variable to redraw the screen, Xcode will throw a massive purple warning: *"Publishing changes from background threads is not allowed."*
To fix this, you must force the ViewModel to jump back to the Main Thread before touching the UI. You do this by marking the ViewModel class with @MainActor.
swift
8. Mini Project: Weather API Skeleton
swift
9. Common Mistakes
-
Forgetting App Transport Security (ATS): Apple rigidly forces all network traffic to be encrypted (
https://). If you try to fetch data from an oldhttp://website, Apple will silently block the request to protect the user's security. You must either use HTTPS, or specifically configure an ATS exception in theInfo.plistfile.
10. Best Practices
-
Never Hardcode URLs in Views: Your UI buttons should never contain string URLs. The View should only know
Task { await viewModel.fetch() }. All URL strings, headers, andURLSessionlogic MUST live securely inside your isolated networking class or ViewModel.
11. Exercises
-
1.
Write a
guard letstatement that safely converts the string"https://google.com"into aURLobject.
-
2.
Inside a Button action, write a
Taskblock.
12. Coding Challenges
Challenge: Modify thefetchRawData() function to purposely hit a broken URL (e.g., "https://thiswebsitedoesnotexist123.com"). Run the app, click the button, and observe how the do-catch block safely intercepts the catastrophic network failure without crashing the app!
13. MCQ Quiz with Answers
Question 1
What is the explicit purpose of the await keyword in modern Swift networking?
Question 2
If a background networking task attempts to directly modify a @Published property that controls the UI, the app will generate a threading error. Which attribute must be applied to the ViewModel class to ensure all UI updates happen safely on the main thread?
14. Interview Questions
-
Q: Explain the necessity of asynchronous programming in mobile architecture. What happens to the user experience if a massive
URLSessiondata download is executed synchronously on the Main Thread?
-
Q: Detail the structural components returned by
URLSession.shared.data(from: url). Why is validating theHTTPURLResponsestatus code (e.g., verifying it is 200) a critical step before processing the raw data?
-
Q: Describe the architectural purpose of the
@MainActorattribute when orchestrating complex background networking tasks within an MVVM architecture.
15. FAQs
Q: Can I use third-party libraries like Alamofire instead of URLSession? A: You can, and historically, many developers did becauseURLSession used to be very ugly to write. However, with the introduction of modern async/await syntax, native URLSession is incredibly clean. Most modern apps no longer require heavy third-party networking libraries.
16. Summary
In Chapter 18, we broke out of the local device and connected our application to the global internet. We constructed safe REST API endpoints utilizing theURL object and orchestrated non-blocking background downloads using native URLSession. We mastered modern asynchronous concurrency architecture, utilizing the async/await syntax to prevent UI freezing, wrapping executions in isolated Task blocks, and guaranteeing thread safety for UI mutations via the powerful @MainActor attribute.
17. Next Chapter Recommendation
Our code downloaded 5,000 bytes of raw, unreadable binary data. You cannot display binary data in aText view. We must decode that binary blob into usable Swift Structs. Proceed to Chapter 19: JSON Parsing and Codable.