An object used to implement turn-based matches between sets of players on Game Center.


class GKTurnBasedMatch : NSObject


A turn-based match uses a store-and-forward approach to share data between the participants. When a player participating in the match performs actions that advance the state of the match, your game describes the new state of the match and decides which player acts next. The next player to act is notified by a push notification. Later, when the next player launches your game, you download the match data from Game Center and continue the match. Players take turns acting (based on whatever internal logic your game implements) until the match ends. One advantage of turn-based matches is that a player may participate in multiple matches simultaneously.

Your game never directly creates GKTurnBasedMatch objects, because a match always represents an existing match that is stored by Game Center. Instead, match objects are created for your game by Game Kit. A match object may hold a newly created match (for a match that has not started yet) or a match that is already under way. Here are ways your game acquires match objects:

A match object contains a few essential properties:

  • The participants property contains an array of GKTurnBasedParticipant objects. Each identifies a player in the match along with the current status of that player.

  • The currentParticipant property contains the GKTurnBasedParticipant object for the player who must act to advance the state of the match.

  • The matchData property holds binary data that describes the current state of the match. The format of the data contained in this object is something you design when you create your game; Game Kit only manages storing and forwarding of this data.

When a player views a particular match, your game should take the match object and call its loadMatchData(completionHandler:) method to retrieve the match data. After the match data is downloaded from Game Center, your game parses the data and displays the game’s user interface. A player may want to view the match even when it is another player’s turn to act; your game should expect this and allow the player to view the state of the match without allowing them to act. When the current player views the match, your game should both show them the state of the match and provide a user interface to allow the player to take a turn. The current player is expected to perform actions that change the state of the match. These actions are based on the internal logic of your game’s rules. The player continues to act until he or she takes an action that requires another person in the match to act in order to advance the match. For example, in a game like Chess, moving a piece on the board and passing control to the other participant happen simultaneously. A more complex game might allow the current player to take multiple actions before the match reaches a state in which control is ceded to another player.

If the player takes an action that is irrevocable (but control is not yet passed to another player), you update the match data by calling the saveCurrentTurn(withMatch:completionHandler:) method.

Once the player takes an action that requires another player to act, your game should determine which player needs to act next. Then, it should encode the state of the match into an NSData object and call the match object’s endTurn(withNextParticipants:turnTimeout:match:completionHandler:) method to set the next player to act. The next player is automatically informed that the match is waiting on his or her actions in order to continue the match.

As the match progresses, players may leave the match. For example, this situation might occur if your game determines that this player has been eliminated from the match. Or your game might offer a user interface to allow players to resign from matches. If the local player resigns from the match and is also the match’s current player, your game must call the match object’s participantQuitInTurn(with:nextParticipant:match:completionHandler:) method. As it does when the player takes a turn, your game passes in the state of the match and the next player to act. Your game also provides a match outcome — essentially, a numerical value that indicates the reason why that player left the match. If your game allows players to resign from matches when it is not that player’s turn, your game calls the match object’s participantQuitOutOfTurn(with:withCompletionHandler:) method to allow the player to leave the match.

Eventually, a match ends. When your game logic dictates that a match should end, your game calls the match object’s endMatchInTurn(withMatch:completionHandler:) method. Your game must ensure that all participants in the match have a valid match outcome before ending the match.

Even after a match ends, its data is still stored on Game Center; this allows players to view previous victories and defeats. A player may choose to delete the match using the standard user interface, but your game can also programmatically remove a match by calling the match object’s remove(completionHandler:) method. In all cases, the decision to delete a match should be made by a player; the remove(completionHandler:) method exists to allow your game to present a custom user interface for viewing and working with matches.

Subclassing Notes

The GKTurnBasedMatch class may not be subclassed.


Retrieving Existing Matches

class func loadMatches(completionHandler: (([GKTurnBasedMatch]?, Error?) -> Void)?)

Loads the turn-based matches involving the local player and creates a match object for each match.

Creating a New Match

func acceptInvite(completionHandler: ((GKTurnBasedMatch?, Error?) -> Void)?)

Programmatically accept an invitation to a turn-based match.

func declineInvite(completionHandler: ((Error?) -> Void)?)

Programmatically decline an invitation to a turn-based match.

func rematch(completionHandler: ((GKTurnBasedMatch?, Error?) -> Void)?)

Create a new turn-based match with the same participants as an existing match.

Retrieving Information About the Match

var participants: [GKTurnBasedParticipant]

Information about the players participating in the match.

var currentParticipant: GKTurnBasedParticipant?

The participant whose turn it is to act next.

var matchData: Data?

Game-specific data that reflects the details of the match.

var matchDataMaximumSize: Int

Returns the limit the Game Center servers place on the size of the match data.

var message: String?

A message displayed to all players in the match.

var creationDate: Date

The date that the match was created.

var matchID: String

A string that uniquely identifies the match.

var status: GKTurnBasedMatch.Status

The current state of the match.

enum GKTurnBasedMatch.Status

The different states that a match can enter.

Retrieving the Match’s Custom Data

func loadMatchData(completionHandler: ((Data?, Error?) -> Void)?)

Loads the game-specific data associated with a match, including all exchanges.

Handling the Current Player’s Turn

Leaving a Match

Deleting a Match from Game Center

func remove(completionHandler: ((Error?) -> Void)?)

Programmatically removes a match from Game Center.

Transferring Information with Exchanges

var GKExchangeTimeoutDefault: TimeInterval

The exchange will timeout after one day if no reply is received.

var GKExchangeTimeoutNone: TimeInterval

The exchange will not timeout.

var activeExchanges: [GKTurnBasedExchange]?

Returns the exchanges that are active for the local player.

var completedExchanges: [GKTurnBasedExchange]?

The exchanges that have been completed and need to be merged by the local participant.

var exchangeDataMaximumSize: Int

The maximum amount of data allowed for an exchange.

var exchangeMaxInitiatedExchangesPerPlayer: Int

Limits the number of exchanges the player can have initiated at once.

var exchanges: [GKTurnBasedExchange]?

The current exchanges that are in progress for the match.

Sending Messages Between Players


Turn Timeouts

Common values for turn timeouts.


enum GKTurnBasedMatch.Outcome

The state the participant was in when they left the match.


Inherits From

Conforms To

See Also

Turn-based Games

class GKTurnBasedExchange

The exchange information sent between players even when a player is not the current player.

class GKTurnBasedExchangeReply

The player’s response to an exchange.

class GKTurnBasedParticipant

The information that describes a participant in a turn-based match.

protocol GKTurnBasedEventListener

The protocol you implement to handle exchanges and match-related events for turn-based games.