iosobjective-cgame-centergkturnbasedmatch

Custom Game Center GKTurnBasedMatchmakerViewController


I'm trying to create a custom view controller to display active games to the user (similar to what Letterpress does).

I am currently trying to subclass GKTurnBasedMatchmakerViewController except I don't know how to hide the default elements (for inviting/showing existing games) and to add my own. I have currently been playing around with logging things to ensure I can access the active matches, current player IDs, etc (which I have recently figured out) but I am stuck on how to start with the interface.

Not sure what code, if any would be relevant at this point. Please let me know.

Looking to: Hide current elements. I'm assuming I can build the interface as I would for any other application after that.

Thanks,


Solution

  • I don't think its a good idea to subclass. Start with a clean UIViewController and build the UI from there. Then implement all the necessary methods.

    Set yourself as delegate:

       [GKTurnBasedEventHandler sharedTurnBasedEventHandler].delegate = self;
    

    so you get delegate event, such as:

    handleInviteFromGameCenter
    handleTurnEventForMatch
    handleMatchEnded
    etc. 
    

    Add a UITableView, and load all matches from Game center:

       [GKTurnBasedMatch loadMatchesWithCompletionHandler:^(NSArray *matches, NSError *error) {
         // Add matches to table view
       }];
    

    Don't forget to authenticate the player first:

    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    localPlayer.authenticateHandler = ^(UIViewController *loginVC, NSError *error)
    {
    
    };
    

    Star a match programatically:

    GKMatchRequest *request = [[GKMatchRequest alloc] init];
    request.minPlayers = 2;
    request.maxPlayers = 2;
    [GKTurnBasedMatch findMatchForRequest:request withCompletionHandler:^(GKTurnBasedMatch *match, NSError *error)
     {
    
     }];
    

    etc. Look at the doc for the full API.