Class

URLSessionUploadTask

A URL session task that uploads data to the network in a request body.

Overview

The URLSessionUploadTask class is a subclass of URLSessionDataTask, which in turn is a concrete subclass of URLSessionTask. The methods associated with the URLSessionUploadTask class are documented in URLSessionTask.

Upload tasks are used for making HTTP requests that require a request body (such as POST or PUT). They behave similarly to data tasks, but you create them by calling different methods on the session that are designed to make it easier to provide the content to upload. As with data tasks, if the server provides a response, upload tasks return that response as one or more NSData objects in memory.

When you create an upload task, you provide an NSURLRequest or NSMutableURLRequest object that contains any additional headers that you might need to send alongside the upload, such as the content type, content disposition, and so on. In iOS, when you create an upload task for a file in a background session, the system copies that file to a temporary location and streams data from there.

While the upload is in progress, the task calls the session delegate’s urlSession(_:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:) method periodically to provide you with status information.

When the upload phase of the request finishes, the task behaves like a data task, calling methods on the session delegate to provide you with the server’s response—headers, status code, content data, and so on.

Relationships

Inherits From

See Also

Adding Upload Tasks to a Session

func uploadTask(with: URLRequest, from: Data)

Creates a task that performs an HTTP request for the specified URL request object and uploads the provided data.

func uploadTask(with: URLRequest, from: Data?, completionHandler: (Data?, URLResponse?, Error?) -> Void)

Creates a task that performs an HTTP request for the specified URL request object, uploads the provided data, and calls a handler upon completion. The task bypasses calls to delegate methods for response and data delivery, and instead provides any resulting NSData, URLResponse, and NSError objects inside the completion handler. Delegate methods for handling authentication challenges, however, are still called.

func uploadTask(with: URLRequest, fromFile: URL)

Creates a task that performs an HTTP request for uploading the specified file.

func uploadTask(with: URLRequest, fromFile: URL, completionHandler: (Data?, URLResponse?, Error?) -> Void)

Creates a task that performs an HTTP request for uploading the specified file, then calls a handler upon completion. The task bypasses calls to delegate methods for response and data delivery, and instead provides any resulting NSData, URLResponse, and NSError objects inside the completion handler. Delegate methods for handling authentication challenges, however, are still called.

func uploadTask(withStreamedRequest: URLRequest)

Creates a task that performs an HTTP request for uploading data based on the specified URL request.

protocol URLSessionDataDelegate

The NSURLSessionDataDelegate protocol defines the methods that a delegate of an URLSession object can implement to handle task-level events specific to data tasks and upload tasks. Your session delegate should also implement the methods in the URLSessionTaskDelegate protocol to handle task-level events that are common to all task types, and methods in the URLSessionDelegate protocol to handle session-level events.