Instance Method

match(_:player:didChange:)

Called when a player connects to or disconnects from the match.

Declaration

optional func match(_ match: GKMatch, player: GKPlayer, didChange state: GKPlayerConnectionState)

Parameters

match

The GKMatch object identifying the match the player is connected to.

player

The GKPlayer object that identifying the player whose state has changed.

state

The GKPlayerConnectionState that the player has moved to.

Discussion

This method is called whenever a member of the match connects or disconnects. GKMatchDelegate shows an example of how to check to see if a player has connected or disconnected from the match. The match delegate defines its own matchStarted property to record whether the match is already in progress. If the match has not started and the count of expected players reaches zero, the method starts the match. In your game, this is where any initial match state would be transmitted to other players or where additional negotiations between the different participants take place.

Even before the match starts, all players already connected to the match can already exchange data with each other. This allows your game to create voice channels (or your own user interface) that allows the players already there to communicate with each other.

Listing 1

Starting a match

- (void)match:(GKMatch *)match player:(GKPlayer *)player didChangeConnectionState:(GKPlayerConnectionState)state
{
    switch (state)
    {
        case GKPlayerStateConnected:
            // Handle a new player connection.
           break;
        case GKPlayerStateDisconnected:
            // A player just disconnected.
           break;
    }
    if (!self.matchStarted && match.expectedPlayerCount == 0)
    {
        self.matchStarted = YES;
        // Handle initial match negotiation.
    }
}

See Also

Receiving State Notifications About Other Players

enum GKPlayerConnectionState

The state of another player in the match.