A peer-to-peer network between a group of devices that are connected to Game Center.


class GKMatch : NSObject


Matches provide transmittal of both voice and game data. Your game never directly allocates GKMatch objects. Instead, it uses the GKMatchmaker class to programmatically find a match with other interested players or a GKMatchmakerViewController object to display a user interface to the player.

After your game receives a match object, set its delegate and then wait until the other participants are connected to the match. You can read the expectedPlayerCount property to determine how many players have not connected to the match.

Each device in the match is identified by the player identifier for the player authenticated on that device. Your game transmits its own data to other players by calling either the sendData(toAllPlayers:with:) method or the send(_:toPlayers:with:) method. To allow voice chat, call voiceChat(withName:) to create one or more voice channels.

When you are finished with the match, call the match’s disconnect() method. Before you release your last reference to a GKMatch object, make sure you set the delegate back to nil. Otherwise a match you thought was long gone can suddenly call match(_:player:didChange:) when you are in the middle of the next game.


Getting and Setting the Delegate

var delegate: GKMatchDelegate?

The delegate for the match.

protocol GKMatchDelegate

The delegate is called when status updates and network data is received from players.

Working with Other Players

var expectedPlayerCount: Int

The remaining number of players who have not yet connected to the match.

var players: [GKPlayer]

An array of GKPlayer objects that represent the players in the match.

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.

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

Transmits data to all players connected to the match.

enum GKMatch.SendDataMode

The mechanism used to transmit data to other players.

Joining a Voice Chat

Finishing the Match

func disconnect()

Disconnects the local player from the match.

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

Create a new match with the list of players from an existing match.

Deprecated Methods and Properties

func chooseBestHostPlayer(completionHandler: (String?) -> Void)

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

var playerIDs: [String]

An array of NSString objects containing the player identifiers for remote players in the match.

func send(Data, toPlayers: [String], with: GKMatch.SendDataMode)

Transmits data to a list of connected players.



Inherits From

Conforms To

See Also

Real-Time Matches

class GKMatchRequest

A set of parameters for a new live or turn-based match.

class GKVoiceChat

A voice channel that allows a set of players in a match to speak with each other.