---
title: "setDataReceiveHandler(_:withContext:)"
framework: gamekit
role: symbol
role_heading: Instance Method
path: "gamekit/gksession/setdatareceivehandler(_:withcontext:)"
---

# setDataReceiveHandler(_:withContext:)

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

## Declaration

```swift
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

Discussion The handler must implement a method with the following signature: - (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:). 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 for more information.

## See Also

### Working with Connected Peers

- [send(_:toPeers:with:)](gamekit/gksession/send(_:topeers:with:).md)
- [sendData(toAllPeers:with:)](gamekit/gksession/senddata(toallpeers:with:).md)
- [disconnectTimeout](gamekit/gksession/disconnecttimeout.md)
- [disconnectFromAllPeers()](gamekit/gksession/disconnectfromallpeers().md)
- [disconnectPeer(fromAllPeers:)](gamekit/gksession/disconnectpeer(fromallpeers:).md)
