Understanding iOS URLSession
30 Jul 2017To facilitate communication with servers in iOS apps, Apple provides the URLSession
API. URLSession
is a fundamental API that underlies popular libraries like Alamofire and SDWebImage for data exchange with servers in iOS app development. URLSession
supports several protocols, including HTTP, and provides features such as authentication, cookie management, and caching.
URLSession’s Request and Response
Like any other HTTP communication, URLSession
has a fundamental structure consisting of a Request
and a Response
. First, Request
can be made directly through a URL
object or by creating a URLRequest
object with options. Next, the Response
can be received in the form of a Completion Handler
for configured Task
or by invoking specified methods via the URLSessionDelegate
.
Generally, for simple responses, you can use Completion Handlers
. However, scenarios like supporting file downloads in the background when the app is in the background state, or situations where default options for authentication and caching are not used, would require using the Delegate
pattern.
Basic Concepts of URLSession
URLSession
typically follows the following life cycle:
- Determine the
Session
configuration and create aSession
. - Set up the URL and Request objects for communication.
- Decide on the
Task
to use and write correspondingCompletion Handlers
orDelegate
methods. - Execute the chosen
Task
. - After the Task is completed, the
Completion Handler
is executed.
1. Session
URLSession
primarily supports three types of Sessions:
Default Session
: The basic session with disk-based caching support.Ephemeral Session
: A session that doesn’t store any data.Background Session
: A session that allows communication even after the app has terminated.
2. Request
You can use URLRequest
to configure how the request to the server should be made. You can specify options such as how data should be cached, which HTTP method to use (GET, POST, etc.), and what content to send.
3. Task
Task objects typically handle URL-based content retrieval after the Session sends the server request. Three types of Tasks are supported:
Data Task
: Used for sending and receiving data objects.Download Task
: Used for downloading data and storing it in files. Supports background downloads.Upload Task
: Used for uploading data in file form.
Using URLSession dataTask
Networking APIs like URLSession
are generally used globally within an app. Therefore, it’s recommended to create a separate module (class) and define static functions inside it, rather than writing methods directly in a ViewController.
// Works properly in Swift 5.1 and iOS 13 environment.
class NetworkHandler {
class func getData(resource: String) {
// Create a session and configure it.
let defaultSession = URLSession(configuration: .default)
guard let url = URL(string: "\(resource)") else {
print("URL is nil")
return
}
// Create a request.
let request = URLRequest(url: url)
// Create a dataTask.
let dataTask = defaultSession.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
// Handle data retrieval errors.
guard error == nil else {
print("Error occur: \(String(describing: error))")
return
}
guard let data = data, let response = response as? HTTPURLResponse, response.statusCode == 200 else {
return
}
// Successful communication results in data being received in the 'data' object.
// If the received data is in JSON format,
// you can serialize the JSON into Swift data types.
// JSON serialization is the process of converting JSON data into a String format that can be used in Swift.
guard let jsonToArray = try? JSONSerialization.jsonObject(with: data, options: []) else {
print("json to Any Error")
return
}
// Perform desired operations.
print(jsonToArray)
}
dataTask.resume()
}
}
NetworkHandler.getData(resource: "http://www.example.com")
Starting with Swift 4, JSON serialization is now possible at the object level (Codable
). For more information, please refer to the link below.