问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/8451817/inviting-a-game-center-friend-to-a-match-programmatically