<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: 14.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "XPC",
  "identifier" : "/documentation/XPC/XPCSession/init(xpcService:targetQueue:options:cancellationHandler:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "XPC"
    ],
    "preciseIdentifier" : "s:3XPC10XPCSessionC10xpcService11targetQueue7options19cancellationHandlerACSS_So17OS_dispatch_queueCSgAC21InitializationOptionsVyAA12XPCRichErrorVcSgtKcfc"
  },
  "title" : "init(xpcService:targetQueue:options:cancellationHandler:)"
}
-->

# init(xpcService:targetQueue:options:cancellationHandler:)

Establishes a connection to an XPC service with the name you specify.

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

## Parameters

`xpcService`

The name of the XPC service to connect to.

`targetQueue`

The dispatch queue to use for session events. You can specify a concurrent dispatch queue. If you specify <doc://com.apple.documentation/documentation/ObjectiveC/nil-227m0>, the session uses `DISPATCH_TARGET_QUEUE_DEFAULT`.

`options`

Attributes the session uses when establishing the connection.

`cancellationHandler`

A closure the system calls when it cancels a session.

## Discussion

If the service isn’t found or is unavailable, the connection fails and this method throws an error.

By default, this method activates the session it creates and the session is ready to accept messages. To create an inactive session, specify [`inactive`](/documentation/XPC/XPCSession/InitializationOptions/inactive) in the `options` parameter.

---

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)