Instance Method

sendData(toAllPlayers:with:)

Transmits data to all players connected to the match.

Declaration

func sendData(toAllPlayers data: Data, with mode: GKMatch.SendDataMode) throws

Parameters

data

The bytes to be sent.

mode

The mechanism used to send the data.

error

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

Return Value

true if the data was successfully queued for transmission; false if the match was unable to queue the data.

Discussion

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 sendData(toAllPlayers:with:) 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

func chooseBestHostingPlayer(completionHandler: (GKPlayer?) -> Void)

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

func send(Data, to: [GKPlayer], dataMode: GKMatch.SendDataMode)

Transmits data to a list of connected players.

enum GKMatch.SendDataMode

The mechanism used to transmit data to other players.