<!--
{
  "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/setIncomingMessageHandler(_:)-5lu26",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "XPC"
    ],
    "preciseIdentifier" : "s:3XPC10XPCSessionC25setIncomingMessageHandleryySE_pSgAA011XPCReceivedE0VcF"
  },
  "title" : "setIncomingMessageHandler(_:)"
}
-->

# setIncomingMessageHandler(_:)

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

```
@preconcurrency func setIncomingMessageHandler(_ incomingMessageHandler: @escaping @Sendable (XPCReceivedMessage) -> (any Encodable)?)
```

## Parameters

`incomingMessageHandler`

A closure that the session invokes when it receives messages. The closure has a parameter that contains the message from the client, and optionally returns an encodable reply message to returns to the client. If the closure returns <doc://com.apple.documentation/documentation/ObjectiveC/nil-227m0>, you can use [`send(message:)`](/documentation/XPC/XPCSession/send(message:)) to reply asynchronously after the closure completes.

## 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)