Class

NSURLSessionTask

A task, like downloading a specific resource, performed in a URL session.

Declaration

@interface NSURLSessionTask : NSObject

Overview

The NSURLSessionTask class is the base class for tasks in a URL session. Tasks are always part of a session; you create a task by calling one of the task creation methods on a NSURLSession instance. The method you call determines the type of task.

  • Use NSURLSession’s dataTaskWithURL: and related methods to create NSURLSessionDataTask instances. Data tasks request a resource, returning the server’s response as one or more NSData objects in memory. They are supported in default, ephemeral, and shared sessions, but are not supported in background sessions.

  • Use NSURLSession’s uploadTaskWithRequest:fromData: and related methods to create NSURLSessionUploadTask instances. Upload tasks are like data tasks, except that they make it easier to provide a request body so you can upload data before retrieving the server’s response. Additionally, upload tasks are supported in background sessions.

  • Use NSURLSession’s downloadTaskWithURL: and related methods to create NSURLSessionDownloadTask instances. Download tasks download a resource directly to a file on disk. Download tasks are supported in any type of session.

  • Use NSURLSession’s streamTaskWithHostName:port: or streamTaskWithNetService: to create NSURLSessionStreamTask instances. Stream tasks establish a TCP/IP connection from a host name and port or a net service object.

After you create a task, you start it by calling its resume method. The session then maintains a strong reference to the task until the request finishes or fails; you don’t need to maintain a reference to the task unless it’s useful for your app’s internal bookkeeping.

Topics

Controlling the Task State

cancel

Cancels the task.

resume

Resumes the task, if it is suspended.

suspend

Temporarily suspends a task.

state

The current state of the task—active, suspended, in the process of being canceled, or completed.

NSURLSessionTaskState

Constants for determining the current state of a task.

priority

The relative priority at which you’d like a host to handle the task, specified as a floating point value between 0.0 (lowest priority) and 1.0 (highest priority).

URL Session Task Priority

Constants for providing task priority hints to a host, used with the priority property.

Obtaining Task Progress

progress

A representation of the overall task progress.

countOfBytesExpectedToReceive

The number of bytes that the task expects to receive in the response body.

countOfBytesReceived

The number of bytes that the task has received from the server in the response body.

countOfBytesExpectedToSend

The number of bytes that the task expects to send in the request body.

countOfBytesSent

The number of bytes that the task has sent to the server in the request body.

NSURLSessionTransferSizeUnknown

The total size of the transfer cannot be determined.

Obtaining General Task Information

currentRequest

The URL request object currently being handled by the task.

originalRequest

The original request object passed when the task was created.

response

The server’s response to the currently active request.

taskDescription

An app-provided description of the current task.

taskIdentifier

An identifier uniquely identifying the task within a given session.

error

An error object that indicates why the task failed.

Scheduling Tasks

countOfBytesClientExpectsToReceive

A best-guess upper bound on the number of bytes the client expects to receive.

countOfBytesClientExpectsToSend

A best-guess upper bound on the number of bytes the client expects to send.

NSURLSessionTransferSizeUnknown

The total size of the transfer cannot be determined.

earliestBeginDate

The earliest date at which the network load should begin.

Relationships

Inherits From

See Also

First Steps

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.

NSURLSession

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

NSURLSessionConfiguration

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