An object that programmatically creates matches with other players and receives match invitations sent by other players.


class GKMatchmaker : NSObject


Matches can be either peer-to-peer or hosted. A peer-to-peer match is fully supported in Game Kit by the GKMatch class. A GKMatch object provides all of the network connections between the devices and routes the network data through Game Center if necessary. In contrast, a hosted match uses matchmaking to find the players for a match, but your game implements its own networking between the participants, routing through your own server if necessary.

To receive invitations from other players, your game must provide an invitation handler. After your game successfully authenticates the local player, it should immediately set the inviteHandler property. The invite handler is called immediately if your game was launched in response to a push notification.

To programmatically search for other players, start by creating a GKMatchRequest object that describes the match you are interested in. Then, call the shared matchmaker’s findMatch(for:withCompletionHandler:) method to create a peer-to-peer match, or its findPlayers(forHostedMatchRequest:withCompletionHandler:) method to create a hosted match. In either case, Game Center matches players into a match and calls your completion handler. If the match does not have enough players (for example, you invited a specific list of players and some declined the invitation), you can create another match request and call the addPlayers(to:matchRequest:completionHandler:) method to add more participants to the match. Once the match is complete, call the finishMatchmaking(for:) method.

If you implement programmatic matchmaking and invite specific players to the match, you should also implement an invitee response handler. This handler is notified as invitations are processed, allowing you to update your game’s custom user interface to display which players are connected to the match.


Retrieving the Shared Matchmaker

class func shared() -> GKMatchmaker

Returns the singleton matchmaker instance.

Receiving Invitations from Other Players

Matching Players

func cancel()

Cancels a pending matchmaking request.

func cancelPendingInvite(to: GKPlayer)

Cancels a pending invitation to another player.

func findMatch(for: GKMatchRequest, withCompletionHandler: ((GKMatch?, Error?) -> Void)?)

Initiates a request to find players for a peer-to-peer match.

func finishMatchmaking(for: GKMatch)

Informs Game Center that programmatic matchmaking has finished.

func queryActivity(completionHandler: ((Int, Error?) -> Void)?)

Initiates a search for activity in all player groups.

Looking for Nearby Players

func startBrowsingForNearbyPlayers(handler: ((GKPlayer, Bool) -> Void)?)

Enables the matchmaking process to find nearby players through Bluetooth or WiFi (same subnet only).

func stopBrowsingForNearbyPlayers()

Ends the search for nearby players.

Deprecated Methods and Properties

func cancelInvite(toPlayer: String)

Cancels a pending invitation to another player.

var inviteHandler: ((GKInvite, [Any]?) -> Void)?

A block to be called when an invitation to join a match is accepted by the local player.

func startBrowsingForNearbyPlayers(reachableHandler: ((String, Bool) -> Void)?)

Enables the matchmaking process to find nearby players through Bluetooth or WiFi (same subnet only).



Inherits From

Conforms To

See Also

Player Invitations

class GKMatchmakerViewController

A user interface for inviting friends to a match or for allowing Game Center to fill the remaining players needed for a match.

class GKTurnBasedMatchmakerViewController

A user interface that allows players to manage the turn-based matches that they are participating in.

class GKInvite

A matchmaking invitation sent by another player to the local player.