A user interface for inviting friends to a match or for allowing Game Center to fill the remaining players needed for a match.
SDKs
- iOS 4.1+
- macOS 10.8+
- Mac Catalyst 13.0+
- tvOS 9.0+
Framework
- Game
Kit
Declaration
class GKMatchmakerViewController : UINavigation Controller
class GKMatchmakerViewController : NSView Controller
Overview
Important
Your game must authenticate a local player before you can use any Game Center classes. If there is no authenticated player, your game receives a GKError
error. For more information on authentication, see Game Center Programming Guide.
To show a matchmaking screen, initialize a new GKMatchmaker
object and set the delegate. Configure the view controller’s other properties to match your specific needs, then present the new view controller and wait for the delegate to be called. The view controller’s delegate is notified when the matchmaking process is completed or canceled. In either situation, you dismiss the view controller. Listing 1 shows how to present the matchmaking user interface to the player.
Showing the standard matchmaking interface
- (IBAction)hostMatch: (id) sender
{
GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.minPlayers = 2;
request.maxPlayers = 2;
GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithMatchRequest:request];
mmvc.matchmakerDelegate = self;
[self presentViewController:mmvc animated:YES completion:nil];
}
If the user is creating the match, your game initializes the matchmaker view controller by creating a GKMatch
object that describes the desired match. This match request is passed to the init(match
method. When this view controller is displayed, the local player can invite other players into the match.
If your game receives an invitation from another player, it receives a GKInvite
object representing the match the player was invited to. You initialize the matchmaker view controller by passing the GKInvite
object received from Game Kit to the init(invite:)
method. When this view controller is presented to the player, the player joins the existing match, but is not allowed to invite others to the match.
In iOS, you present and dismiss the view controller from another view controller in your game, using the methods provided by the UIView
class. In macOS, you use the GKDialog
class to present and dismiss the view controller.