<!--
{
  "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/URLSessionTaskDelegate",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(pl)NSURLSessionTaskDelegate"
  },
  "title" : "URLSessionTaskDelegate"
}
-->

# URLSessionTaskDelegate

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

```
protocol URLSessionTaskDelegate : URLSessionDelegate
```

## Overview

You use this protocol in one of two ways, depending on how you use a [`URLSession`](/documentation/Foundation/URLSession):

- If you create tasks with Swift’s `async`-`await` syntax, using methods like [`bytes(for:delegate:)`](/documentation/Foundation/URLSession/bytes(for:delegate:)) and [`data(for:delegate:)`](/documentation/Foundation/URLSession/data(for:delegate:)), you pass a `delegate` argument of this type. The delegate receives callbacks for things like task progress, while the call point awaits the completion of the task.
- If you add tasks to the session with methods like [`dataTask(with:)`](/documentation/Foundation/URLSession/dataTask(with:)-10dy7) and [`downloadTask(with:)`](/documentation/Foundation/URLSession/downloadTask(with:)-1onj), then you implement this protocol’s methods in a [`delegate`](/documentation/Foundation/URLSession/delegate) you set on the session. This session delegate may also implement other protocols as appropriate, like [`URLSessionDownloadDelegate`](/documentation/Foundation/URLSessionDownloadDelegate) and [`URLSessionDataDelegate`](/documentation/Foundation/URLSessionDataDelegate). You can also assign a delegate of this type directly to the task to intercept callbacks before the task delivers them to the session’s delegate.

> Note:
> Your ``doc://com.apple.foundation/documentation/Foundation/URLSession`` object doesn’t need to have a delegate. If you don’t assign a delegate, the session uses a system-provided delegate. In this case, you must provide a completion callback or use the Swift `async`-`await` methods to obtain the data.

## Topics

### Handling task life cycle changes

[`urlSession(_:task:didCompleteWithError:)`](/documentation/Foundation/URLSessionTaskDelegate/urlSession(_:task:didCompleteWithError:))

Tells the delegate that the task finished transferring data.

### Handling redirects

[`urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)`](/documentation/Foundation/URLSessionTaskDelegate/urlSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:))

Tells the delegate that the remote server requested an HTTP redirect.

### Working with upload tasks

[`urlSession(_:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:)`](/documentation/Foundation/URLSessionTaskDelegate/urlSession(_:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:))

Periodically informs the delegate of the progress of sending body content to the server.

[`urlSession(_:task:needNewBodyStream:)`](/documentation/Foundation/URLSessionTaskDelegate/urlSession(_:task:needNewBodyStream:))

Tells the delegate when a task requires a new request body stream to send to the remote server.

### Handling authentication challenges

[`urlSession(_:task:didReceive:completionHandler:)`](/documentation/Foundation/URLSessionTaskDelegate/urlSession(_:task:didReceive:completionHandler:))

Requests credentials from the delegate in response to an authentication request from the remote server.

[`URLSession.AuthChallengeDisposition`](/documentation/Foundation/URLSession/AuthChallengeDisposition)

Constants passed by session or task delegates to the provided continuation block in response to an authentication challenge.

### Handling delayed and waiting tasks

[`urlSession(_:task:willBeginDelayedRequest:completionHandler:)`](/documentation/Foundation/URLSessionTaskDelegate/urlSession(_:task:willBeginDelayedRequest:completionHandler:))

Tells the delegate that a delayed URL session task will now begin loading.

[`URLSession.DelayedRequestDisposition`](/documentation/Foundation/URLSession/DelayedRequestDisposition)

The action to take on a delayed URL session task.

[`urlSession(_:taskIsWaitingForConnectivity:)`](/documentation/Foundation/URLSessionTaskDelegate/urlSession(_:taskIsWaitingForConnectivity:))

Tells the delegate that the task is waiting until suitable connectivity is available before beginning the network load.

### Collecting task metrics

[`urlSession(_:task:didFinishCollecting:)`](/documentation/Foundation/URLSessionTaskDelegate/urlSession(_:task:didFinishCollecting:))

Tells the delegate that the session finished collecting metrics for the task.

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

An object encapsulating the metrics for a 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)