GameCenter Invitation Handler
trying to implement a multiplayer. Using the sample from Game Center - Sending and receiving data.
Everything seems okay, but in apple documentation there is also said about invitation handler.
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {
// Insert application-specific code here to clean up any games in progress.
if (acceptedInvite) {
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite] autorelease];
mmvc.matchmakerDelegate = self;
[self presentModalViewController:mmvc animated:YES];
} else if (playersToInvite) {
GKMatchRequest *request = [[[GKMatchRequest alloc] init] autorelease];
request.minPlayers = 2;
request.maxPlayers = 4;
request.playersToInvite = playersToInvite;
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithMatchRequest:request] autorelease];
mmvc.matchmakerDelegate = self;
[self presentModalViewController:mmvc animated:YES];
}
};
The problem is quite simple: I do 开发者_如何学JAVAnot know where to add this code.
As stated in the docs
Your application should set the invitation handler as early as possible after your application is launched; an appropriate place to set the handler is in the completion block you provided that executes after the local player is authenticated.
Somewhere in your code, you should have authenticated the local player with something like this
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
if (error == nil) {
// Insert your piece of code here
} else {
// Handle the error
}
}];
Hope that helps
My code is below, and it works very well. In the authenticateLocalUser
, add the code below:
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) { // Add for invite handler
// Insert application-specific code here to clean up any games in progress.
if (acceptedInvite) {
GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite] ;
mmvc.matchmakerDelegate = self;
// [self presentModalViewController:mmvc animated:YES];
[_delegate matchStart];
} else if (playersToInvite) {
GKMatchRequest *request = [[GKMatchRequest alloc] init] ;
request.minPlayers = 2;
request.maxPlayers = 2;
request.playersToInvite = playersToInvite;
GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithMatchRequest:request] ;
mmvc.matchmakerDelegate = self;
// [self presentModalViewController:mmvc animated:YES];
[_delegate matchStart];
}
};
[self callDelegateOnMainThread:@selector(processGameCenterAuth:) withArg:NULL error:error];
}];
精彩评论