[null, {"document_url": "https://cocoacasts.com/networking-in-swift-meet-the-urlsession-family", "unformatted_src": "https://cdn.cocoacasts.com/networking-in-swift/nis-ep-1-meet-the-urlsession-family/images/figure-project-setup-1.jpg", "src": "https://cdn.cocoacasts.com/networking-in-swift/nis-ep-1-meet-the-urlsession-family/images/figure-project-setup-1.jpg", "formatted_filename": "figure project setup", "alt_text": "Setting Up the Project"}, {"document_url": "https://cocoacasts.com/networking-in-swift-meet-the-urlsession-family", "unformatted_src": "https://cdn.cocoacasts.com/networking-in-swift/nis-ep-1-meet-the-urlsession-family/images/figure-project-setup-2.jpg", "src": "https://cdn.cocoacasts.com/networking-in-swift/nis-ep-1-meet-the-urlsession-family/images/figure-project-setup-2.jpg", "formatted_filename": "figure project setup", "alt_text": "Setting Up the Project"}, null, {"document_url": "https://cocoacasts.com/networking-in-swift-meet-the-urlsession-family", "unformatted_src": "https://cdn.cocoacasts.com/networking-in-swift/nis-ep-1-meet-the-urlsession-family/images/figure-creating-user-interface-1.jpg", "src": "https://cdn.cocoacasts.com/networking-in-swift/nis-ep-1-meet-the-urlsession-family/images/figure-creating-user-interface-1.jpg", "formatted_filename": "figure creating user interface", "alt_text": "Creating the User Interface"}, null, {"document_url": "https://cocoacasts.com/networking-in-swift-meet-the-urlsession-family", "unformatted_src": "https://cdn.cocoacasts.com/networking-in-swift/nis-ep-1-meet-the-urlsession-family/images/figure-simulator-1.jpg", "src": "https://cdn.cocoacasts.com/networking-in-swift/nis-ep-1-meet-the-urlsession-family/images/figure-simulator-1.jpg", "formatted_filename": "figure simulator", "alt_text": "Downloading and Displaying an Image"}, null]
Networking In Swift

Meet the URLSession Family

by Bart Jacobs | Jan 31, 2018 | Networking



AFNetworking has always been one of my favorite libraries and Alamofire is just as easy to like. Since the introduction of URLSession in iOS 7 and macOS Mavericks, I've been more reluctant to include either libraries in my projects.

The reason is simple. The URLSession API offers an easy-to-use, modern API for networking. It offers flexibility and several features many developers have been asking for. If a project can do without another dependency, then that's something worth considering.

URLSession is the successor of NSURLConnection. For many years, NSURLConnection has been the workhorse for networking on iOS and macOS. Most developers used or created a wrapper around NSURLConnection to hide the less enjoyable aspects of the API.

In addition to being a class, URLSession is a technology that provides the infrastructure for networking, exposed through a modern and elegant API. In this series, I introduce you to the URLSession stack. You learn how easy it is to get started with URLSession and you discover that URLSession exposes a flexible API that should meet anyone's networking needs.


The URLSession family includes a number of core classes. The URLSession class is the key component of the URLSession stack. It's used to create and configure network requests.

Another important class is URLSessionConfiguration. A session configuration object is used to configure a URLSession instance. The session is in charge of managing requests. The session configuration defines the behavior of the session. This is especially interesting for uploading and downloading data.

The workhorses of URLSession are URLSessionTask and its concrete subclasses. The subclasses you interact with most are:

URLSessionDataTask and URLSessionDownloadTask directly inherit from URLSessionTask. URLSessionUploadTask is a subclass of URLSessionDataTask.

A URLSessionTask object is always associated with a session. You can create a URLSessionTask object by asking the session for one. This is illustrated by the following example. We discuss the details a bit later in this episode.

Fetching Data

Now that we have met the family, it's time to create a request to see how the various pieces fit together. Create a new project in Xcode based on the Single View App template. Set Product Name to Networking and Language to Swift. To illustrate how the URLSession stack works, we fetch an image from the web and display it in an image view. This simple example shows you how easy it is to create a request, schedule it, and handle its response.

Open Main.storyboard and add an image view to the View Controller Scene. Add the required constraints to pin the image view to the edges of its superview. Open the Attributes Inspector and set Mode to Aspect Fit. Open ViewController.swift and create an outlet for the image view. Revisit Main.storyboard and connect the outlet to the image view.

Open ViewController.swift and locate the viewDidLoad() method.

We obtain a reference to a URLSession instance by invoking shared on the URLSession class. This returns a shared session object, a singleton, that uses the global cache, cookie storage, and credential storage. There are other options to create a session, but the shared session is fine for this example.

We create a URL instance and use it to create a request, an instance of the URLRequest class.

To make a request, we ask the session for a data task by invoking dataTask(with:completionHandler:). This method accepts two arguments, the request and a closure. The closure accepts three arguments:

The closure is executed when the request completes, successfully or unsuccessfully.

We use optional binding to safely unwrap the data that is returned to us and we use it to instantiate a UIImage object. We update the image view with the resulting image.

It's important to point out that we explicitly update the image view on the main thread. The closure we pass to dataTask(with:completionHandler:) is invoked on a background thread and, therefore, we are required to dispatch updating the image view to the main queue.

Last but not least, we schedule the data task by invoking resume() on the data task. This is often overlooked by developers that are new to URLSession.

Run the application in the simulator or on a physical device to see the result. 
Using URLSession is much easier than NSURLConnection. It's clear Apple decided it was time to rethink networking on its platforms when they created URLSession. In this episode, you learned about the very basics of URLSession. In the next episodes, we take a look at the more advanced options of the URLSession API.


