iOS Developer Library

Developer

GameKit Framework Reference GKMatchmakerViewControllerDelegate Protocol Reference

Options
Deployment Target:

On This Page
Language:

GKMatchmakerViewControllerDelegate

Inherits From


Not Applicable

Import Statement


Swift

import GameKit

Objective-C

@import GameKit;

Availability


Available in iOS 4.0 and later

A class implements the GKMatchmakerViewControllerDelegate protocol to receive notifications from a GKMatchmakerViewController object. The delegate is called if a new match has been successfully created, if the user cancels matchmaking, and if an error occurs. In all three cases, the delegate should dismiss the view controller.

  • Called when a peer-to-peer match is found.

    Declaration

    Swift

    optional func matchmakerViewController(_ viewController: GKMatchmakerViewController!, didFindMatch match: GKMatch!)

    Objective-C

    - (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match

    Parameters

    viewController

    The view controller that performed the matchmaking.

    match

    A completed match.

    Discussion

    This method is called when the view controller’s hosted property is NOfalse. Although optional in the protocol, if your game attaches a delegate to the view controller for a peer-to-peer match, the view controller expects your game to provide an implementation of this method.

    Listing 1 shows a possible implementation of the delegate method. This method assigns the match object to a property on the object and then adds the object as the match object’s delegate. At this stage of the process, all the players have been found, but it is possible for the match object to be returned before all players are successfully connected. If all the players are already connected to the match, the game recognizes this and immediately begins the match. Otherwise, it does nothing; the match delegate starts the match when the last player connects.

    Listing 1Implementing the match found method
    • - (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match
    • {
    • [self dismissViewControllerAnimated:YES completion:nil];
    • self.myMatch = match; // Use a retaining property to retain the match.
    • match.delegate = self;
    • if (!self.matchStarted && match.expectedPlayerCount == 0)
    • {
    • self.matchStarted = YES;
    • // Insert game-specific code to start the match.
    • }
    • }

    Import Statement

    Objective-C

    @import GameKit;

    Swift

    import GameKit

    Availability

    Available in iOS 4.1 and later

  • Called when a hosted match is found. (required)

    Declaration

    Swift

    optional func matchmakerViewController(_ viewController: GKMatchmakerViewController!, didFindHostedPlayers players: [AnyObject]!)

    Objective-C

    - (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindHostedPlayers:(NSArray *)players

    Parameters

    viewController

    The view controller that performed the matchmaking.

    players

    An array of GKPlayer objects containing the player identifier for the matched players.

    Discussion

    This method is called when the view controller’s hosted property is YEStrue. Although optional in the protocol, if your game attaches a delegate to the view controller for a hosted match, the view controller expects your game to provide an implementation of this method.

    The view controller returns the list of players to your game by calling this method. Your game is responsible for connecting these players to your own server and then using that server to relay messages between the players.

    Import Statement

    Objective-C

    @import GameKit;

    Swift

    import GameKit

    Availability

    Available in iOS 8.0 and later

  • Called when a hosted match is found.

    Declaration

    Swift

    optional func matchmakerViewController(_ viewController: GKMatchmakerViewController!, didFindPlayers playerIDs: [AnyObject]!)

    Objective-C

    - (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindPlayers:(NSArray *)playerIDs

    Parameters

    viewController

    The view controller that performed the matchmaking.

    playerIDs

    An array of NSString objects containing player identifier for the matched players.

    Discussion

    This method is called when the view controller’s hosted property is YEStrue. Although optional in the protocol, if your game attaches a delegate to the view controller for a hosted match, the view controller expects your game to provide an implementation of this method.

    The view controller returns the list of players to your game by calling this method. Your game is responsible for connecting these players to your own server and then using that server to relay messages between the players.

    Import Statement

    Objective-C

    @import GameKit;

    Swift

    import GameKit

    Availability

    Available in iOS 4.1 and later

    Deprecated in iOS 8.0

  • Called when the user cancels the matchmaking request (required)

    Declaration

    Swift

    func matchmakerViewControllerWasCancelled(_ viewController: GKMatchmakerViewController!)

    Objective-C

    - (void)matchmakerViewControllerWasCancelled:(GKMatchmakerViewController *)viewController

    Parameters

    viewController

    The view controller that received the cancellation.

    Import Statement

    Objective-C

    @import GameKit;

    Swift

    import GameKit

    Availability

    Available in iOS 4.1 and later

  • Called when the view controller encounters an unrecoverable error. (required)

    Declaration

    Swift

    func matchmakerViewController(_ viewController: GKMatchmakerViewController!, didFailWithError error: NSError!)

    Objective-C

    - (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFailWithError:(NSError *)error

    Parameters

    viewController

    The view controller that received the error.

    error

    An error object that describes the error.

    Import Statement

    Objective-C

    @import GameKit;

    Swift

    import GameKit

    Availability

    Available in iOS 4.1 and later

  • Called when a player in a hosted match accepts the invitation. (required)

    Declaration

    Swift

    optional func matchmakerViewController(_ viewController: GKMatchmakerViewController!, hostedPlayerDidAccept player: GKPlayer!)

    Objective-C

    - (void)matchmakerViewController:(GKMatchmakerViewController *)viewController hostedPlayerDidAccept:(GKPlayer *)player

    Parameters

    viewController

    The view controller that accepted the invitation.

    player

    The GKPlayer object that identifies the accepting player.

    Discussion

    After a player accepts an invitation, that player’s device should connect to your server. Once the connection is established, your game should call the view controller’s setHostedPlayer:didConnect: method to update the player’s connection status.

    Import Statement

    Objective-C

    @import GameKit;

    Swift

    import GameKit

    Availability

    Available in iOS 8.0 and later

  • Called when a player in a hosted match accepts the invitation.

    Declaration

    Swift

    optional func matchmakerViewController(_ viewController: GKMatchmakerViewController!, didReceiveAcceptFromHostedPlayer playerID: String!)

    Objective-C

    - (void)matchmakerViewController:(GKMatchmakerViewController *)viewController didReceiveAcceptFromHostedPlayer:(NSString *)playerID

    Parameters

    viewController

    The view controller that accepted the invitation.

    playerID

    The identifier of the accepting player.

    Discussion

    After a player accepts an invitation, that player’s device should connect to your server. Once the connection is established, your game should call the view controller’s setHostedPlayer:connected: method to update the player’s connection status.

    Import Statement

    Objective-C

    @import GameKit;

    Swift

    import GameKit

    Availability

    Available in iOS 5.0 and later

    Deprecated in iOS 8.0