<!--
{
  "availability" : [
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "XPC",
  "identifier" : "/documentation/XPC/XPCSession/init(endpoint:targetQueue:options:incomingMessageHandler:cancellationHandler:)-2jmkk",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "XPC"
    ],
    "preciseIdentifier" : "s:3XPC10XPCSessionC8endpoint11targetQueue7options22incomingMessageHandler012cancellationI0AcA11XPCEndpointV_So17OS_dispatch_queueCSgAC21InitializationOptionsVAA13XPCDictionaryVSgAQcSgyAA12XPCRichErrorVcSgtKcfc"
  },
  "title" : "init(endpoint:targetQueue:options:incomingMessageHandler:cancellationHandler:)"
}
-->

# init(endpoint:targetQueue:options:incomingMessageHandler:cancellationHandler:)

Creates a new session object representing a connection to the xpc endpoint.

```
@preconcurrency convenience init(endpoint: XPCEndpoint, targetQueue: DispatchQueue? = nil, options: XPCSession.InitializationOptions = .none, incomingMessageHandler: (@Sendable (XPCDictionary) -> XPCDictionary?)? = nil, cancellationHandler: (@Sendable (XPCRichError) -> Void)? = nil) throws
```

## Parameters

`endpoint`

The endpoint to create a session with.

`targetQueue`

The GCD queue onto which session events will be submitted.
This may be a concurrent queue. This parameter is optional, if the target queue is
not specified the target queue will be libdispatch’s default target queue, defined as
`DISPATCH_TARGET_QUEUE_DEFAULT`.

`options`

Additional attributes which which to create the session.

`incomingMessageHandler`

The handler block to be called when a message
originated by the peer is received through the provided session. This parameter is optional.
See `setIncomingMessageHandler()`

`cancellationHandler`

The cancellation handler block that will be executed when this session is cancelled.
This parameter is optional. See `setCancellationHandler()`

## Return Value

Returns a new session object. The returned session is activated by
default and can be used to send messages.

## Discussion> Throws: A `XPCRichError` describing the details of the error that occurred.

This will throw a XPCRichError if the specified endpoint is invalid or unavailable.

---

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)