<!--
{
  "availability" : [
    "iOS: 9.0.0 -",
    "iPadOS: 9.0.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.11.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/URLSessionStreamDelegate",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(pl)NSURLSessionStreamDelegate"
  },
  "title" : "URLSessionStreamDelegate"
}
-->

# URLSessionStreamDelegate

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

```
protocol URLSessionStreamDelegate : URLSessionTaskDelegate
```

## Overview

In addition to these methods, be sure to implement the methods in the [`URLSessionTaskDelegate`](/documentation/Foundation/URLSessionTaskDelegate) and [`URLSessionDelegate`](/documentation/Foundation/URLSessionDelegate) protocols to handle events common to all task types and session-level events, respectively.

> Note: A ``doc://com.apple.foundation/documentation/Foundation/URLSession`` object need not have a delegate. If no delegate is assigned, a system-provided delegate is used, and you must provide a completion callback to obtain the data.

## Topics

### Handling rerouting

[`-  URLSession:betterRouteDiscoveredForStreamTask:`](/documentation/Foundation/URLSessionStreamDelegate/urlSession(_:betterRouteDiscoveredFor:))

Tells the delegate that a better route to the host has been detected for the stream.

### Completing stream capture

[`-  URLSession:streamTask:didBecomeInputStream:outputStream:`](/documentation/Foundation/URLSessionStreamDelegate/urlSession(_:streamTask:didBecome:outputStream:))

Tells the delegate that the stream task has been completed as a result of the stream task calling the [`captureStreams()`](/documentation/Foundation/URLSessionStreamTask/captureStreams()) method.

### Handling closing events

[`-  URLSession:readClosedForStreamTask:`](/documentation/Foundation/URLSessionStreamDelegate/urlSession(_:readClosedFor:))

Tells the delegate that the read side of the underlying socket has been closed.

[`-  URLSession:writeClosedForStreamTask:`](/documentation/Foundation/URLSessionStreamDelegate/urlSession(_:writeClosedFor:))

Tells the delegate that the write side of the underlying socket has been closed.

## Relationships

### Inherits From

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

[`Sendable`](/documentation/Swift/Sendable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

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

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

---

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)