<!--
{
  "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/URLSessionDataTask",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSURLSessionDataTask"
  },
  "title" : "URLSessionDataTask"
}
-->

# URLSessionDataTask

A URL session task that returns downloaded data directly to the app in memory.

```
class URLSessionDataTask
```

## Overview

A [`URLSessionDataTask`](/documentation/Foundation/URLSessionDataTask) is a concrete subclass of [`URLSessionTask`](/documentation/Foundation/URLSessionTask). The methods in the [`URLSessionDataTask`](/documentation/Foundation/URLSessionDataTask) class are documented in [`URLSessionTask`](/documentation/Foundation/URLSessionTask).

A data task returns data directly to the app (in memory) as one or more `NSData` objects. When you use a data task:

- During upload of the body data (if your app provides any), the session periodically calls its delegate’s [`urlSession(_:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:)`](/documentation/Foundation/URLSessionTaskDelegate/urlSession(_:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:)) method with status information.
- After receiving an initial response, the session calls its delegate’s [`urlSession(_:dataTask:didReceive:completionHandler:)`](/documentation/Foundation/URLSessionDataDelegate/urlSession(_:dataTask:didReceive:completionHandler:)) method to let you examine the status code and headers, and optionally convert the data task into a download task.
- During the transfer, the session calls its delegate’s [`urlSession(_:dataTask:didReceive:)`](/documentation/Foundation/URLSessionDataDelegate/urlSession(_:dataTask:didReceive:)) method to provide your app with the content as it arrives.
- Upon completion, the session calls its delegate’s [`urlSession(_:dataTask:willCacheResponse:completionHandler:)`](/documentation/Foundation/URLSessionDataDelegate/urlSession(_:dataTask:willCacheResponse:completionHandler:)) method to let you determine whether the response should be cached.

For examples of using data tasks for fetching and uploading data, see [Fetching website data into memory](/documentation/Foundation/fetching-website-data-into-memory) and [Uploading data to a website](/documentation/Foundation/uploading-data-to-a-website).

---

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)