<!--
{
  "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/URLSessionDelegate/urlSession(_:didBecomeInvalidWithError:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(pl)NSURLSessionDelegate(im)URLSession:didBecomeInvalidWithError:"
  },
  "title" : "urlSession(_:didBecomeInvalidWithError:)"
}
-->

# urlSession(_:didBecomeInvalidWithError:)

Tells the URL session that the session has been invalidated.

```
optional func urlSession(_ session: URLSession, didBecomeInvalidWithError error: (any Error)?)
```

## Parameters

`session`

The session object that was invalidated.

`error`

The error that caused invalidation, or `nil` if the invalidation was explicit.

## Discussion

If you invalidate a session by calling its [`finishTasksAndInvalidate()`](/documentation/Foundation/URLSession/finishTasksAndInvalidate()) method, the session waits until after the final task in the session finishes or fails before calling this delegate method. If you call the [`invalidateAndCancel()`](/documentation/Foundation/URLSession/invalidateAndCancel()) method, the session calls this delegate method immediately.

---

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)