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

# init(service:targetQueue:options:incomingSessionHandler:)

Creates the server side of an XPC service using the specified service name.

```
@preconcurrency init(service: String, targetQueue: DispatchQueue? = nil, options: XPCListener.InitializationOptions = .none, incomingSessionHandler: @escaping @Sendable (XPCListener.IncomingSessionRequest) -> XPCListener.IncomingSessionRequest.Decision) throws
```

## Parameters

`service`

The Mach service or XPC service name that clients use to connect to the service.

`targetQueue`

The dispatch queue that events arrive on. This may be a concurrent queue. If <doc://com.apple.documentation/documentation/ObjectiveC/nil-227m0>, the listeners uses `DISPATCH_TARGET_QUEUE_DEFAULT`.

`options`

Configuration options for the listener, such as creating it in an inactive state.

`incomingSessionHandler`

A handler that the system calls when a client connects to the XPC service.

## Discussion

Listener creation fails if the XPC service isn’t found or is unavailable.

When a client connects to your service, the system invokes the `incomingSessionHandler` with a request that you must either accept or reject. To accept the incoming request, choose one of the following two approaches:

- For simple protocols, use [`accept(incomingMessageHandler:cancellationHandler:)`](/documentation/XPC/XPCListener/IncomingSessionRequest/accept(incomingMessageHandler:cancellationHandler:)-8rodk) or [`accept(incomingMessageHandler:cancellationHandler:)`](/documentation/XPC/XPCListener/IncomingSessionRequest/accept(incomingMessageHandler:cancellationHandler:)-48c3k) to provide a closure that receives the incoming message directly.
- For more complex protocols that delegate message handling to a different object, use `accept(_:)` to provide a closure that returns a [`XPCPeerHandler`](/documentation/XPC/XPCPeerHandler). The peer handler object receives incoming messages from the client directly.

When the `incomingSessionHandler` returns, the system automatically activates the peer session unless you explicitly reject it or pass the [`inactive`](/documentation/XPC/XPCListener/InitializationOptions/inactive) initialzation option.

---

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)