<!--
{
  "availability" : [
    "iOS: 7.0.0 -",
    "iPadOS: 7.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.9.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URLSessionTask",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLSessionTask"
  },
  "title" : "URLSessionTask"
}
-->

# URLSessionTask

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

```
class URLSessionTask
```

## Overview

The [`URLSessionTask`](/documentation/Foundation/URLSessionTask) 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 [`URLSession`](/documentation/Foundation/URLSession) instance. The method you call determines the type of task.

- Use ``doc://com.apple.foundation/documentation/Foundation/URLSession``‘s ``doc://com.apple.foundation/documentation/Foundation/URLSession/dataTask(with:)-10dy7`` and related methods to create ``doc://com.apple.foundation/documentation/Foundation/URLSessionDataTask`` 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 ``doc://com.apple.foundation/documentation/Foundation/URLSession``‘s ``doc://com.apple.foundation/documentation/Foundation/URLSession/uploadTask(with:from:)`` and related methods to create ``doc://com.apple.foundation/documentation/Foundation/URLSessionUploadTask`` 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 ``doc://com.apple.foundation/documentation/Foundation/URLSession``’s ``doc://com.apple.foundation/documentation/Foundation/URLSession/downloadTask(with:)-1onj`` and related methods to create ``doc://com.apple.foundation/documentation/Foundation/URLSessionDownloadTask`` instances. Download tasks download a resource directly to a file on disk. Download tasks are supported in any type of session.
- Use ``doc://com.apple.foundation/documentation/Foundation/URLSession``’s ``doc://com.apple.foundation/documentation/Foundation/URLSession/streamTask(withHostName:port:)`` or ``doc://com.apple.foundation/documentation/Foundation/URLSession/streamTask(with:)`` to create ``doc://com.apple.foundation/documentation/Foundation/URLSessionStreamTask`` 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()`](/documentation/Foundation/URLSessionTask/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.

> Note:
> All task properties support key-value observing.

## Topics

### Controlling the task state

[`cancel()`](/documentation/Foundation/URLSessionTask/cancel())

Cancels the task.

[`resume()`](/documentation/Foundation/URLSessionTask/resume())

Resumes the task, if it is suspended.

[`suspend()`](/documentation/Foundation/URLSessionTask/suspend())

Temporarily suspends a task.

[`state`](/documentation/Foundation/URLSessionTask/state-swift.property)

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

[`URLSessionTask.State`](/documentation/Foundation/URLSessionTask/State-swift.enum)

Constants for determining the current state of a task.

[`priority`](/documentation/Foundation/URLSessionTask/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](/documentation/Foundation/url-session-task-priority)

Constants for providing task priority hints to a host, used with the [`priority`](/documentation/Foundation/URLSessionTask/priority) property.

### Obtaining task progress

[`progress`](/documentation/Foundation/URLSessionTask/progress)

A representation of the overall task progress.

[`countOfBytesExpectedToReceive`](/documentation/Foundation/URLSessionTask/countOfBytesExpectedToReceive)

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

[`countOfBytesReceived`](/documentation/Foundation/URLSessionTask/countOfBytesReceived)

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

[`countOfBytesExpectedToSend`](/documentation/Foundation/URLSessionTask/countOfBytesExpectedToSend)

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

[`countOfBytesSent`](/documentation/Foundation/URLSessionTask/countOfBytesSent)

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

[`NSURLSessionTransferSizeUnknown`](/documentation/Foundation/NSURLSessionTransferSizeUnknown)

The total size of the transfer cannot be determined.

### Obtaining general task information

[`currentRequest`](/documentation/Foundation/URLSessionTask/currentRequest)

The URL request object currently being handled by the task.

[`originalRequest`](/documentation/Foundation/URLSessionTask/originalRequest)

The original request object passed when the task was created.

[`response`](/documentation/Foundation/URLSessionTask/response)

The server’s response to the currently active request.

[`taskDescription`](/documentation/Foundation/URLSessionTask/taskDescription)

An app-provided string value for the current task.

[`taskIdentifier`](/documentation/Foundation/URLSessionTask/taskIdentifier)

An identifier uniquely identifying the task within a given session.

[`error`](/documentation/Foundation/URLSessionTask/error)

An error object that indicates why the task failed.

### Determining task behavior

[`prefersIncrementalDelivery`](/documentation/Foundation/URLSessionTask/prefersIncrementalDelivery)

A Boolean value that determines whether to deliver a partial response body in increments.

### Using a task-specific delegate

[`delegate`](/documentation/Foundation/URLSessionTask/delegate)

A delegate specific to the task.

[`URLSessionTaskDelegate`](/documentation/Foundation/URLSessionTaskDelegate)

A protocol that defines methods that URL session instances call on their delegates to handle task-level events.

### Scheduling tasks

[`countOfBytesClientExpectsToReceive`](/documentation/Foundation/URLSessionTask/countOfBytesClientExpectsToReceive)

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

[`countOfBytesClientExpectsToSend`](/documentation/Foundation/URLSessionTask/countOfBytesClientExpectsToSend)

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

[`NSURLSessionTransferSizeUnknown`](/documentation/Foundation/NSURLSessionTransferSizeUnknown)

The total size of the transfer cannot be determined.

[`earliestBeginDate`](/documentation/Foundation/URLSessionTask/earliestBeginDate)

The earliest date at which the network load should begin.

### Deprecated

[`init()`](/documentation/Foundation/URLSessionTask/init())

Initializes an empty URL sesson task.

[`new()`](/documentation/Foundation/URLSessionTask/new())

Creates a new URL session task.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)
