Instance Method


Transmits data to all players connected to the match.


- (BOOL)sendDataToAllPlayers:(NSData *)data withDataMode:(GKMatchSendDataMode)mode error:(NSError * _Nullable *)error;



The bytes to be sent.


The mechanism used to send the data.


If the data could not be queued, on return, this parameter holds an NSError object describing the error.

Return Value

YES if the data was successfully queued for transmission; NO if the match was unable to queue the data.


The match queues the data and transmits it when the network becomes available.

Listing 1 shows how a game might send a position update to the other participants. The method fills a structure with position data, wraps it in an NSData object, and then calls the match’s sendDataToAllPlayers:withDataMode:error: method. When the method returns without reporting an error, the message has been queued and will be sent when the network is available.

Listing 1

Sending a position to all players

- (void) sendPosition
    NSError *error;
    PositionPacket msg;
    msg.messageKind = PositionMessage;
    msg.x = currentPosition.x;
    msg.y = currentPosition.y;
    NSData *packet = [NSData dataWithBytes:&msg length:sizeof(PositionPacket)];
    [match sendDataToAllPlayers: packet withDataMode: GKMatchSendDataUnreliable error:&error];
    if (error != nil)
        // Handle the error.

See Also

Sending Data to Other Players

- chooseBestHostingPlayerWithCompletionHandler:

Determines the best player in the game to act as the server for a client-server match.

- sendData:toPlayers:dataMode:error:

Transmits data to a list of connected players.


The mechanism used to transmit data to other players.