<!--
{
  "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",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "XPC"
    ],
    "preciseIdentifier" : "s:3XPC10XPCSessionC"
  },
  "title" : "XPCSession"
}
-->

# XPCSession

A type that sends messages to a server process.

```
class XPCSession
```

## Overview

XPC sessions are stateful connections you use to send structured messages to a separate process. Once established, a session remains active until one side of the connection cancels it, at which point the system invalidates the connection.

## Topics

### Creating a session

[`init(xpcService:targetQueue:options:incomingMessageHandler:cancellationHandler:)`](/documentation/XPC/XPCSession/init(xpcService:targetQueue:options:incomingMessageHandler:cancellationHandler:)-407h2)

Establishes a connection to an XPC service with the name and decodable message handler you specify.

[`init(xpcService:targetQueue:options:incomingMessageHandler:cancellationHandler:)`](/documentation/XPC/XPCSession/init(xpcService:targetQueue:options:incomingMessageHandler:cancellationHandler:)-9f4u0)

Establishes a connection to an XPC service with the name and received message handler you specify.

[`init(xpcService:targetQueue:options:incomingMessageHandler:cancellationHandler:)`](/documentation/XPC/XPCSession/init(xpcService:targetQueue:options:incomingMessageHandler:cancellationHandler:)-bel3)

Establishes a connection to an XPC service with the name and dictionary message handler you specify.

[`init(xpcService:targetQueue:options:cancellationHandler:)`](/documentation/XPC/XPCSession/init(xpcService:targetQueue:options:cancellationHandler:))

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

[`init(machService:targetQueue:options:incomingMessageHandler:cancellationHandler:)`](/documentation/XPC/XPCSession/init(machService:targetQueue:options:incomingMessageHandler:cancellationHandler:)-l3rz)

Establishes a connection to a launch agent or launch daemon with the name and decodable message handler you specify.

[`init(machService:targetQueue:options:incomingMessageHandler:cancellationHandler:)`](/documentation/XPC/XPCSession/init(machService:targetQueue:options:incomingMessageHandler:cancellationHandler:)-2xuyi)

Establishes a connection to a launch agent or launch daemon with the name and received message handler you specify.

[`init(machService:targetQueue:options:incomingMessageHandler:cancellationHandler:)`](/documentation/XPC/XPCSession/init(machService:targetQueue:options:incomingMessageHandler:cancellationHandler:)-6jz7y)

Establishes a connection to a launch agent or launch daemon with the name and dictionary message handler you specify.

[`init(machService:targetQueue:options:cancellationHandler:)`](/documentation/XPC/XPCSession/init(machService:targetQueue:options:cancellationHandler:))

Establishes a connection to a launch agent or launch daemon with the name you specify.

[`XPCSession.InitializationOptions`](/documentation/XPC/XPCSession/InitializationOptions)

Options that control the session’s configuration.

[`setTargetQueue(_:)`](/documentation/XPC/XPCSession/setTargetQueue(_:))

Sets the target dispatch queue on an inactive session for processing messages.

### Managing the life cycle

[`activate()`](/documentation/XPC/XPCSession/activate())

Activates a session so you can send messages.

[`setIncomingMessageHandler(_:)`](/documentation/XPC/XPCSession/setIncomingMessageHandler(_:)-2ukdh)

Sets a closure to receive incoming decodable messages for a session.

[`setIncomingMessageHandler(_:)`](/documentation/XPC/XPCSession/setIncomingMessageHandler(_:)-5lu26)

Sets a closure to receive incoming received messages for a session.

[`setIncomingMessageHandler(_:)`](/documentation/XPC/XPCSession/setIncomingMessageHandler(_:)-75ou9)

Sets a closure to receive incoming dictionary messages for a session.

[`cancel(reason:)`](/documentation/XPC/XPCSession/cancel(reason:))

Cancels a session, discarding any unsent messages.

[`setCancellationHandler(_:)`](/documentation/XPC/XPCSession/setCancellationHandler(_:))

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

### Sending messages

[`send(_:)`](/documentation/XPC/XPCSession/send(_:))

Sends an encodable message over the session to the destination service.

[`send(_:replyHandler:)`](/documentation/XPC/XPCSession/send(_:replyHandler:)-3wjln)

Sends an encodable message over the session to the destination service, using the closure you specify to handle a reply and rich error.

[`send(_:replyHandler:)`](/documentation/XPC/XPCSession/send(_:replyHandler:)-9an0u)

Sends an encodable message over the session to the destination service, using the closure you specify to handle a reply.

[`send(message:)`](/documentation/XPC/XPCSession/send(message:))

Sends a dictionary message over the session to the destination service.

[`send(message:replyHandler:)`](/documentation/XPC/XPCSession/send(message:replyHandler:))

Sends a message asynchronously over the session to the destination service, calling a closure after receiving a reply.

[`sendSync(_:)`](/documentation/XPC/XPCSession/sendSync(_:)-8a284)

Sends an encodable message over the session to the destination service, blocking the caller until receiving a reply message.

[`sendSync(_:)`](/documentation/XPC/XPCSession/sendSync(_:)-88u0s)

Sends an encodable message over the session to the destination service, blocking the caller until receiving an encodable reply message.

[`sendSync(message:)`](/documentation/XPC/XPCSession/sendSync(message:))

Sends a dictionary message over the session to the destination service, blocking the caller until receiving a reply.



---

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)