GKTurnBasedMatch Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/GameKit.framework |
| Availability | Available in iOS 5.0 and later. |
| Companion guide | |
| Declared in | GKTurnBasedMatch.h |
Overview
The GKTurnBasedMatch class allows your game to implement turn-based matches between sets of players on Game Center. 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:
To allow the player to see a standard user interface for turn-based matches, use the
GKTurnBasedMatchmakerViewControllerclass.To programmatically find a new match for the player to join, call the
findMatchForRequest:withCompletionHandler:class method.To programmatically load a collection of match objects for matches the local player is already participating in, call the
loadMatchesWithCompletionHandler:class method.To receive notifications when it is the local player’s turn in a match, attach a delegate to the
GKTurnBasedEventHandlersingleton object.
A match object contains a few essential properties:
The
participantsproperty contains an array ofGKTurnBasedParticipantobjects. Each identifies a player in the match along with the current status of that player.The
currentParticipantproperty contains theGKTurnBasedParticipantobject for the player must act to advance the state of the match.The
matchDataproperty 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 loadMatchDataWithCompletionHandler: 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 saveCurrentTurnWithMatchData: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 endTurnWithNextParticipants:turnTimeout:matchData: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 participantQuitInTurnWithOutcome:nextParticipant:matchData: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 participantQuitOutOfTurnWithOutcome: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 endMatchInTurnWithMatchData: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 removeWithCompletionHandler: method. In all cases, the decision to delete a match should be made by a player; the removeWithCompletionHandler: 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.
Tasks
Retrieving Existing Matches
Creating a New Match
-
+ findMatchForRequest:withCompletionHandler: -
– acceptInviteWithCompletionHandler: -
– declineInviteWithCompletionHandler: -
– rematchWithCompletionHandler:
Retrieving Information About the Match
-
creationDateproperty -
currentParticipantproperty -
matchIDproperty -
messageproperty -
participantsproperty -
statusproperty -
matchDataMaximumSizeproperty -
matchDataproperty
Retrieving the Match’s Custom Data
Handling the Current Player’s Turn
-
– saveCurrentTurnWithMatchData:completionHandler: -
– endTurnWithNextParticipants:turnTimeout:matchData:completionHandler: -
– endTurnWithNextParticipant:matchData:completionHandler:Deprecated in iOS 6.0
Leaving a Match
-
– participantQuitInTurnWithOutcome:nextParticipants:turnTimeout:matchData:completionHandler: -
– participantQuitOutOfTurnWithOutcome:withCompletionHandler: -
– participantQuitInTurnWithOutcome:nextParticipant:matchData:completionHandler:Deprecated in iOS 6.0
Ending a Match
Deleting a Match From Game Center
Properties
creationDate
The date that the match was created. (read-only)
Availability
- Available in iOS 5.0 and later.
Declared In
GKTurnBasedMatch.hcurrentParticipant
The participant whose turn it is to act next. (read-only)
Discussion
The current participant is the only participant that is allowed to update the match data.
Availability
- Available in iOS 5.0 and later.
Declared In
GKTurnBasedMatch.hmatchData
Game-specific data that reflects the details of the match. (read-only)
Discussion
Although Game Center knows who is participating in the match and who is expected to act next, it does not know anything about your game’s internal logic. Your game provides the match data and all the programming logic required to interpret it. This data should include the current state of the game and provide any necessary details about what actions the current player is expected to take. It can also be helpful for your game to record information about recent moves made by other players. The game can then replay those moves visually for the player to show exactly how the match reached the state it is in now.
Your game never directly updates the match state associated with this property. Instead, when the data is updated to reflect the actions of the current player, your game serializes the updated state into memory and calls one of the match’s instance methods that transmit the updated state to Game Center.
The value of this property is nil until after your game calls the loadMatchDataWithCompletionHandler: method and the load task is complete. After this task completes, the matchData property holds the data that the last player to act transmitted to Game Center.
Availability
- Available in iOS 5.0 and later.
See Also
Declared In
GKTurnBasedMatch.hmatchDataMaximumSize
Returns the limit the Game Center servers place on the size of the match data. (read-only)
Discussion
Game Kit returns an error if your game sends updated data larger than this value.
Availability
- Available in iOS 6.0 and later.
Declared In
GKTurnBasedMatch.hmatchID
A string that uniquely identifies the match. (read-only)
Discussion
This string is not intended to be displayed to players. Your game should use this string whenever it needs to identify a specific match. For example, if you want your game to store additional information on a device or in iCloud, it might store it in a database using the match ID as a key.
Availability
- Available in iOS 5.0 and later.
Declared In
GKTurnBasedMatch.hmessage
A message displayed to all players in the match.
Discussion
The message property is displayed by the standard user interface; this allows your game to use the message to inform players of the current state of the match.
Availability
- Available in iOS 5.0 and later.
Declared In
GKTurnBasedMatch.hparticipants
Information about the players participating in the match. (read-only)
Discussion
The elements of this array are GKTurnBasedParticipant objects representing each participant in the match. Your game uses these objects to retrieve more information about the participants in the match. Your game also uses one of the objects in this array as a parameter whenever it calls a method that sets a different participant to act in the match.
The size of the array and the order in which the participants appear in the array are set when the match is first created, and never changes. When a match is first created, some participants may not hold actual players yet. Game Center searches for a player to fill that spot in the match only after your game sets that participant as the current player.
Availability
- Available in iOS 5.0 and later.
Declared In
GKTurnBasedMatch.hstatus
The current state of the match. (read-only)
Availability
- Available in iOS 5.0 and later.
Declared In
GKTurnBasedMatch.hClass Methods
findMatchForRequest:withCompletionHandler:
Programmatically searches for a new match to join.
Parameters
- request
A match request that specifies the properties that the new match must fulfill.
- completionHandler
A block to be called after the match is successfully created.
The block receives the following parameters:
- match
A newly initialized match object that contains a list of players for the match. If an error occurred, this value is
nil.- error
If an error occurred, this error object describes the error. If the operation was completed successfully, the value is
nil.
Discussion
When this method is called, it creates a background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. Keep in mind that the completion handler may be called on a thread other than the one originally used to invoke the method. This means that the code in your block needs to be thread-safe.
This method may either create a new match or it may place the player into an existing match that needs a new player to advance the match further. Regardless of how the player is placed in the match, the local player is always the current participant in the returned match. Your game should immediately display the match in its user interface and allow the player to take a turn.
Availability
- Available in iOS 5.0 and later.
Declared In
GKTurnBasedMatch.hloadMatchesWithCompletionHandler:
Loads the turn-based matches involving the local player and creates a match object for each match.
Parameters
- completionHandler
A block to be called after the matches are retrieved from the server.
The block receives the following parameters:
- matches
An array of match objects for matches that the local player is playing in, or
nilif there are no matches to load. If an error occurred, this value may be non-nil. In this case, the array holds whatever match data could be retrieved from Game Center before the error occurred.- error
If an error occurred, this error object describes the error. If the operation was completed successfully, the value is
nil.
Discussion
When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. Keep in mind that the completion handler may be called on a thread other than the one originally used to invoke the method. This means that the code in your block needs to be thread-safe.
Availability
- Available in iOS 5.0 and later.
Declared In
GKTurnBasedMatch.hloadMatchWithID:withCompletionHandler:
Loads a specific match.
Parameters
- matchID
The identifier for the turn-based match.
- completionHandler
A block to be called after the match is retrieved from the server.
The block receives the following parameters:
- match
If the operation completed successfully, this parameter holds the match. If an error occurred, the value is
nil.- error
If an error occurred, this error object describes the error. If the operation was completed successfully, the value is
nil.
Discussion
When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. Keep in mind that the completion handler may be called on a thread other than the one originally used to invoke the method. This means that the code in your block needs to be thread-safe.
Availability
- Available in iOS 6.0 and later.
Declared In
GKTurnBasedMatch.hInstance Methods
acceptInviteWithCompletionHandler:
Programmatically accept an invitation to a turn-based match.
Parameters
- completionHandler
A block to be called after the match is successfully created.
The block receives the following parameters:
- match
A newly initialized match object that contains a list of players for the match. If an error occurred, this value is
nil.- error
If an error occurred, this error object describes the error. If the operation was completed successfully, the value is
nil.
Discussion
When this method is called, it creates a background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. Keep in mind that the completion handler may be called on a thread other than the one originally used to invoke the method. This means that the code in your block needs to be thread-safe.
Availability
- Available in iOS 6.0 and later.
Declared In
GKTurnBasedMatch.hdeclineInviteWithCompletionHandler:
Programmatically decline an invitation to a turn-based match.
Parameters
- completionHandler
A block to be called after the match is successfully created.
The block receives the following parameter:
- error
If an error occurred, this error object describes the error. If the operation was completed successfully, the value is
nil.
Discussion
When this method is called, it creates a background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. Keep in mind that the completion handler may be called on a thread other than the one originally used to invoke the method. This means that the code in your block needs to be thread-safe.
Availability
- Available in iOS 6.0 and later.
Declared In
GKTurnBasedMatch.hendMatchInTurnWithMatchData:completionHandler:
Ends the match.
Parameters
- matchData
A serialized blob of data reflecting the end state for the match.
- completionHandler
A block to be called after the match is successfully ended.
The block receives the following parameters:
- error
If an error occurred, this error object describes the error. If the operation was completed successfully, the value is
nil.
Discussion
Calling this method ends the match for all players. This method may only be called by the current participant. Before your game calls this method, the matchOutcome property on each participant object stored in the participants property must have been set to a value other than GKTurnBasedMatchOutcomeNone.
When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. Keep in mind that the completion handler may be called on a thread other than the one originally used to invoke the method. This means that the code in your block needs to be thread-safe.
Availability
- Available in iOS 5.0 and later.
Declared In
GKTurnBasedMatch.hendTurnWithNextParticipants:turnTimeout:matchData:completionHandler:
Updates the data stored on Game Center for the current match.
Parameters
- nextParticipants
An array of participant objects reflecting the order in which the players should act next. Each object in the array must be one of the objects stored in the match’s
participantsproperty.- timeout
The length of time the next player has to complete their turn.
- matchData
A serialized blob of data reflecting the game-specific state for the match.
- completionHandler
A block to be called after the data is uploaded to Game Center.
The block receives the following parameters:
- error
If an error occurred, this error object describes the error. If the operation was completed successfully, the value is
nil.
Discussion
If the next player to act does not take their turn in the specified interval, the next player in the array receives a notification to act. This process continues until a player takes a turn or the last player in the list is notified.
When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. Keep in mind that the completion handler may be called on a thread other than the one originally used to invoke the method. This means that the code in your block needs to be thread-safe.
Availability
- Available in iOS 6.0 and later.
Declared In
GKTurnBasedMatch.hloadMatchDataWithCompletionHandler:
Loads the game-specific data associated with a match.
Parameters
- completionHandler
A block to be called after the scores are retrieved from the server.
The block receives the following parameters:
- matchData
The data stored on Game Center that reflects the current state of the match. If an error occurred, this value is
nil.- error
If an error occurred, this error object describes the error. If the operation was completed successfully, the value is
nil.
Discussion
When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. Keep in mind that the completion handler may be called on a thread other than the one originally used to invoke the method. This means that the code in your block needs to be thread-safe.
Availability
- Available in iOS 5.0 and later.
Declared In
GKTurnBasedMatch.hparticipantQuitInTurnWithOutcome:nextParticipants:turnTimeout:matchData:completionHandler:
Resigns the current player from the match without ending the match.
Parameters
- matchOutcome
The end outcome of the current player in the match.
- nextParticipants
An array of participant objects reflecting the order in which the players should act next. Each object in the array must be one of the objects stored in the match’s
participantsproperty.- timeout
The length of time the next player has to complete their turn.
- matchData
A serialized blob of data reflecting the game-specific state for the match.
- completionHandler
A block to be called after the data is uploaded to the server.
The block receives the following parameters:
- error
If an error occurred, this error object describes the error. If the operation was completed successfully, the value is
nil.
Discussion
Your game calls this method on an instance of your game that is processing the current player’s turn, but that player has left the match. For example, the player may have willingly resigned from the match or that player may have been eliminated by the other players (based on your game’s internal logic).
If the next player to act does not take their turn in the specified interval, the next player in the array receives a notification to act. This process continues until a player takes a turn or the last player in the list is notified.
When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. Keep in mind that the completion handler may be called on a thread other than the one originally used to invoke the method. This means that the code in your block needs to be thread-safe.
Availability
- Available in iOS 6.0 and later.
Declared In
GKTurnBasedMatch.hparticipantQuitOutOfTurnWithOutcome:withCompletionHandler:
Resigns the player from the match when that player is not the current player. This action does not end the match
Parameters
- matchOutcome
The end outcome of the current player in the match.
- completionHandler
A block to be called after the player’s status is updated on Game Center.
The block receives the following parameters:
- error
If an error occurred, this error object describes the error. If the operation was completed successfully, the value is
nil.
Discussion
If the local player decided they wanted to resign from the match but is not the current participant in the match, your game calls this method.
When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. Keep in mind that the completion handler may be called on a thread other than the one originally used to invoke the method. This means that the code in your block needs to be thread-safe.
Availability
- Available in iOS 5.0 and later.
Declared In
GKTurnBasedMatch.hrematchWithCompletionHandler:
Create a new turn-based match with the same participants as an existing match.
Parameters
- completionHandler
A block to be called after the match is successfully created.
The block receives the following parameters:
- match
A newly initialized match object that contains a list of players for the match. If an error occurred, this value is
nil.- error
If an error occurred, this error object describes the error. If the operation was completed successfully, the value is
nil.
Discussion
When this method is called, it creates a background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. Keep in mind that the completion handler may be called on a thread other than the one originally used to invoke the method. This means that the code in your block needs to be thread-safe.
Availability
- Available in iOS 6.0 and later.
Declared In
GKTurnBasedMatch.hremoveWithCompletionHandler:
Programmatically removes a match from Game Center.
Parameters
- completionHandler
A block to be called after the scores are retrieved from the server.
The block receives the following parameters:
- error
If an error occurred, this error object describes the error. If the operation was completed successfully, the value is
nil.
Discussion
Even after a player’s participation in a match ends, the data associated with the match continues to be stored on Game Center. Storing the data on Game Center allows the player to continue to watch the match’s progress, or even see the final state of the match when it ends. However, players may also want to delete matches that they have finished playing. If you choose not to use the standard matchmaker user interface, your game should offer the ability to delete a finished match from Game Center. When a player chooses to delete a match from Game Center, call this method. It is a programming error to call this method on a match that has the local player as an active participant.
When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. Keep in mind that the completion handler may be called on a thread other than the one originally used to invoke the method. This means that the code in your block needs to be thread-safe.
When the task completes, the match is no longer visible to the local player whose device made the call. Other players involved in the match still see the match.
Availability
- Available in iOS 5.0 and later.
Declared In
GKTurnBasedMatch.hsaveCurrentTurnWithMatchData:completionHandler:
Update the match data without advancing the game to another player.
Parameters
- matchData
A serialized blob of data reflecting the game-specific state for the match.
- completionHandler
A block to be called after the data is uploaded to Game Center.
The block receives the following parameters:
- error
If an error occurred, this error object describes the error. If the operation was completed successfully, the value is
nil.
Discussion
This method updates the match data stored on Game Center. Call this method when the current player takes an action that advances the state of the match but does not end the player’s turn. For example, if your game has a fog-of-war mechanic, you might call this method when the player revealed new information on the map.
When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. Keep in mind that the completion handler may be called on a thread other than the one originally used to invoke the method. This means that the code in your block needs to be thread-safe.
Availability
- Available in iOS 6.0 and later.
Declared In
GKTurnBasedMatch.hConstants
GKTurnBasedMatchStatus
The different states that a match can enter.
enum {
GKTurnBasedMatchStatusUnknown = 0,
GKTurnBasedMatchStatusOpen = 1,
GKTurnBasedMatchStatusEnded = 2,
GKTurnBasedMatchStatusMatching = 3
};
typedef NSInteger GKTurnBasedMatchStatus;
Constants
GKTurnBasedMatchStatusUnknownThe match is in an unexpected state.
Available in iOS 5.0 and later.
Declared in
GKTurnBasedMatch.h.GKTurnBasedMatchStatusOpenThe match is currently being played.
Available in iOS 5.0 and later.
Declared in
GKTurnBasedMatch.h.GKTurnBasedMatchStatusEndedThe match has been completed.
Available in iOS 5.0 and later.
Declared in
GKTurnBasedMatch.h.GKTurnBasedMatchStatusMatchingGame Center is still searching for other players to join the match.
Available in iOS 5.0 and later.
Declared in
GKTurnBasedMatch.h.
Turn Timeouts
Common values for turn timeouts.
extern NSTimeInterval GKTurnTimeoutDefault; extern NSTimeInterval GKTurnTimeoutNone;
Constants
© 2012 Apple Inc. All Rights Reserved. (Last updated: 2012-12-13)