<!--
{
  "availability" : [
    "iOS: 3.0.0 - 7.0.0",
    "iPadOS: 3.0.0 - 7.0.0",
    "macOS: 10.8.0 - 10.10.0",
    "visionOS: 1.0.0 - 1.0.0",
    "watchOS: 2.0.0 - 2.0.0"
  ],
  "documentType" : "symbol",
  "framework" : "GameKit",
  "identifier" : "/documentation/GameKit/GKSession/setDataReceiveHandler(_:withContext:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "GameKit"
    ],
    "preciseIdentifier" : "c:objc(cs)GKSession(im)setDataReceiveHandler:withContext:"
  },
  "title" : "setDataReceiveHandler(_:withContext:)"
}
-->

# setDataReceiveHandler(_:withContext:)

Sets the object that handles data received from other peers connected to the session.

```
func setDataReceiveHandler(_ handler: Any!, withContext context: UnsafeMutableRawPointer!)
```

## Parameters

`handler`

The object you want the session to call when it receives data from other peers.

`context`

Arbitrary data to be passed to each invocation of the handler.

## Discussion

The handler must implement a method with the following signature:

```objc
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context;
```

where *data* contains the bytes received from a remote peer, *peer* is a string that identifies the peer, *session* is the session that received the data, and *context* is the same context that was passed into the original call to [`setDataReceiveHandler(_:withContext:)`](/documentation/GameKit/GKSession/setDataReceiveHandler(_:withContext:)).

> Important:
> Data received from other peers should be treated as *untrusted* data. Be sure to validate the data you receive from the session and write your code carefully to avoid security vulnerabilities. See the [Secure Coding Guide](https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Introduction.html#//apple_ref/doc/uid/TP40002415) for more information.

---

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)