Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inviting a Game Center friend to a match programmatically

Does GameKit allow you to invite a specific Game Center friend to a match, programmatically, i.e. without presenting the GC ViewController? The following handleInviteFromGameCenter documentation seems to imply that you can populate GKMatchRequest.playersToInvite and use it with [GKTurnBasedMatch findMatchForRequest]:

When your delegate receives this message, your application should create a new GKMatchRequest object and assign the playersToInvite parameter to the match request’s playersToInvite property. Then, your application can either call the GKTurnBasedMatch class method findMatchForRequest:withCompletionHandler: to find a match programmatically or it can use the request to instantiate a new GKTurnBasedMatchmakerViewController to show a user interface to the player.

But I'm finding that when findMatchForRequest calls my completion block with the populated match, the GameCenter ID I passed to it is not set as the 2nd player. Instead it's empty and the status is "matching". And therefore, when I call endTurnWithNextParticipant, it succeeds, but the invite isn't received on my 2nd device. This illustrates what I'm doing:

GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease]; 
request.minPlayers = 2;
request.maxPlayers = 2;
request.playersToInvite = [NSArray arrayWithObjects: otherPlayerGCID,nil ];

[GKTurnBasedMatch findMatchForRequest:request 
                  withCompletionHandler:^(GKTurnBasedMatch *match, NSError *error) 
{
    if (error) 
        NSLog(@"returned from fimdmatch but with error");
    else if (match != nil) {
        NSLog(@"match returned success and match populated");
        NSArray* otherPlayers = [match participants];
        if (otherPlayers.count>1) {
           NSData* placeholder = [@"no data" dataUsingEncoding:NSUTF8StringEncoding];
           [match endTurnWithNextParticipant:[otherPlayers objectAtIndex:1] 
                  matchData:placeholder 
                  completionHandler:^(NSError *error) 
           {
              if (error) 
                 NSLog(@"returned from END TURN but with error");
              else
                 NSLog(@"returned from  END TURN successfully");
           }];     

        }
     }
     else
        System::log("match returned success but match NOT populated");
}];

And like the person who seems to be having a similar problem here Game Center inviting friends progammatically, if I insert a call to the view controller, in my case GKTurnBasedMatchmakerViewController, all seems to work.

Thanks.

UPDATED: I did see in an Apple Developer's presentation on turn-based GC, a mention of something like, "If you want to invite a GC friend, we ask that you go through the GC view controller.

Any insight appreciated. Thanks again.

like image 791
leontx Avatar asked Dec 09 '11 21:12

leontx


People also ask

How do you invite a friend to play a game on Game Center?

Click your Game Center ID. Click Add Friends. Enter the phone number or email of the person that you want to invite to be friends in Game Center, or click the Add button to invite one of your contacts. Once that person accepts the request through the Messages app, you'll be able to see them on your list of friends.

How do you send a game invite on Iphone?

In Messages, tap the link. In a supported game, tap the Game Center profile picture, tap Friends, then tap Friend Requests. In the App Store, tap. or your profile picture at the top right, tap Game Center, then tap Friend Requests.


1 Answers

Wanted to share what I learned on this: As of iOS 5 there is not a way to invite a game center friend to play a game without going through the pre-defined GKTurnBasedMatchmakerViewController flow, which is optimized for starting a match in real time, guiding the user through three different screens.

After being urged to do so by apple dev support, I did submit a feature request to be able to invoke a simple, one-page view controller that would allow the user to send an invite / "recommend game" message via game center.

UPDATE FOR iOS 6: Happy to report that it looks like this has been addressed in iOS 6. My original programmatic (non-UI) example above now works as originally expected.

like image 88
leontx Avatar answered Nov 19 '22 05:11

leontx