URL Loading System

Interact with URLs and communicate with servers using standard Internet protocols.

Overview

The URL Loading System provides access to resources identified by URLs, using standard protocols like https or custom protocols you create. Loading is performed asynchronously, so your app can remain responsive and handle incoming data or errors as they arrive.

You use a URLSession instance to create one or more URLSessionTask instances, which can fetch and return data to your app, download files, or upload data and files to remote locations. To configure a session, you use a URLSessionConfiguration object, which controls behavior like how to use caches and cookies, or whether to allow connections on a cellular network.

You can use one session repeatedly to create tasks. For example, a web browser might have separate sessions for regular and private browsing use, where the private session doesn’t cache its data. Figure 1 shows how two sessions with these configurations can then create multiple tasks.

Figure 1

Creating tasks from URL sessions

Figure showing two scenarios, default browsing and private browsing, each with a URL Session creating multiple URL Session Tasks. In the default browsing case, the URL Session contains a default configuration. In the private browsing case, it contains an ephemeral configuration.

Each session is associated with a delegate to receive periodic updates (or errors). The default delegate calls a completion handler block that you provide; if you choose to provide your own custom delegate, this block is not called.

You can configure a session to run in the background, so that while the app is suspended, the system can download data on its behalf and wake up the app to deliver the results.

Topics

First Steps

Configure and create sessions, then use them to create tasks that interact with URLs.

Fetching Website Data into Memory

Receive data directly into memory by creating a data task from a URL session.

Uploading Data to a Website

Post data from your app to servers.

Downloading Files in the Background

Create tasks that download files while your app is inactive.

class URLSession

An object that coordinates a group of related network data transfer tasks.

class URLSessionConfiguration

A configuration object that defines behavior and policies for a URL session.

class URLSessionTask

A task, such as downloading a specific resource, to be undertaken by a URL session.

Requests and Responses

struct URLRequest

A URL load request that is independent of protocol or URL scheme.

class URLResponse

The metadata associated with the response to a URL load request, independent of protocol and URL scheme.

class HTTPURLResponse

The metadata associated with the response to an HTTP protocol URL load request.

Cache Behavior

Accessing Cached Data

Control how URL requests make use of previously cached data.

class CachedURLResponse

A cached response to a URL request.

class URLCache

An object that maps URL requests to cached response objects.

Authentication and Credentials

Handling an Authentication Challenge

Respond appropriately when a server demands authentication for a URL request.

class URLAuthenticationChallenge

A challenge from a server requiring authentication from the client.

class URLCredential

An authentication credential consisting of authentication information specific to the type of credential and the type of persistent storage to use, if any.

class URLCredentialStorage

An object that manages the credential storage.

class URLProtectionSpace

A server or an area on a server, commonly referred to as a realm, that requires authentication.

Cookies

class HTTPCookie

An object that represents an HTTP cookie. It is an immutable object, initialized from a dictionary containing the cookie attributes.

class HTTPCookieStorage

A singleton object (shared instance) that manages storage of cookies.

Errors

struct URLError

Error codes returned by URL loading APIs.

URL Loading System Error Info Keys

Recognize these keys from the user info dictionary of error objects produced by URL Loading APIs.

Legacy

Legacy URL Loading Systems

Migrate your code away from using these legacy objects.

See Also

Networking

Bonjour

Advertise services for easy discovery on local networks, or discover services advertised by others.