Class

GKMatch

A GKMatch object provides a peer-to-peer network between a group of devices that are connected to Game Center. Matches provide transmit 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.

Overview

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.

Symbols

Getting and Setting the Delegate

var delegate: GKMatchDelegate?

The delegate for the match.

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.

var playerIDs: [String]

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

Deprecated

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: GKMatchSendDataMode)

Transmits data to a list of connected players.

func sendData(toAllPlayers: Data, with: GKMatchSendDataMode)

Transmits data to all players connected to the match.

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

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

Deprecated
func send(Data, toPlayers: [String], with: GKMatchSendDataMode)

Transmits data to a list of connected players.

Deprecated

Joining a Voice Chat

func voiceChat(withName: String)

Joins a voice channel.

Finishing the Match

func disconnect()

Disconnects the local player from the match.

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

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

Constants

GKMatchSendDataMode

The mechanism used to transmit data to other players.

Relationships

Inherits From

Conforms To