<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: -",
    "watchOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "XPC",
  "identifier" : "/documentation/XPC/XPCSession/setCancellationHandler(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "XPC"
    ],
    "preciseIdentifier" : "s:3XPC10XPCSessionC22setCancellationHandleryyyAA12XPCRichErrorVcF"
  },
  "title" : "setCancellationHandler(_:)"
}
-->

# setCancellationHandler(_:)

Sets a closure the session calls when it’s canceled.

```
@preconcurrency func setCancellationHandler(_ cancellationHandler: @escaping @Sendable (XPCRichError) -> Void)
```

## Parameters

`cancellationHandler`

A closure that receives an error indicating why the session was canceled.

## Discussion

> Important:
> Only call this method on an inactive session.

---

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)