GameCenter Invitation Handler

trying to implement multiplayer. Using a sample from Game Center - sending and receiving data .

Everything looks fine, but the Apple documentation also talks about the 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 pretty simple: I don't know where to add this code.

+6
source share
2 answers

As indicated in the docs

Your application should establish an invitation as soon as possible after your application has been launched; A suitable place to install the handler is in the completion block you provided, which is executed after the local player is authenticated.

Somewhere in your code you had to authenticate a 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

+11
source

My code is below and it works very well. In authenticateLocalUser add the following code:

 [[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]; }]; 
+1
source

All Articles