How do I handle invitations in the game center if a friend has not advanced far enough in the application to reach the installInvitationHandler method?

I am creating a game in real time and am confused about how to deal with invitations to the game? For example, a player on one device can invite his friends to a match, and then an invitation banner will appear on the friends' devices. They can use the banner and accept the invitation. Now this works great if a friend launched the application earlier and installed the invitation handler below (installed in the second application view controller)

- (void) installInvitationHandler { [GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) { // Insert game-specific code here to clean up any game in progress. if (acceptedInvite) { if(self.myConnectingVC) return; else if(self.myMatchmakerVC) { [self dismissViewControllerAnimated:YES completion:^{ self.myMatchmakerVC = nil; GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite]; mmvc.matchmakerDelegate = self; self.myConnectingVC = mmvc; [self presentViewController:mmvc animated:YES completion:nil]; }]; } else { GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite]; mmvc.matchmakerDelegate = self; [self presentViewController:mmvc animated:YES completion:nil]; } } else if (playersToInvite) { [self createMatchWithPlayersToInvite:playersToInvite]; } }; } 

The problem is, what should I do if a friend has never run an application before or if a friend has not advanced far enough in the application to reach the installInvitationHandler method? If this happens, if a friend clicks on the invitation banner, the application will open, but it will not accept the invitation.

+7
source share
2 answers

You must add an invitation to participate immediately after launching the application. The GKInvite object is passed when the handler is called.

 [GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) { //By the time this block is called, you have already received the //invite, it passed as a parameter if (acceptedInvite) { //This is your invite } else if (playersToInvite) { //The player has launched your app from the Game Center app, invite these players. } }; 
+1
source

I found that it is best to attach the Handler prompt as a completion block to the authenticateHandler from GKLocalPlayer.

This is for iOS 6.0 onwards ...

 [GKLocalPlayer localPlayer].authenticateHandler = ^(UIViewController *viewController, NSError *error) { if([GKLocalPlayer localPlayer].authenticated) _gameCenterFeaturesEnabled = YES; // Present the view controller if needed if (![GKLocalPlayer localPlayer].authenticated) { if(viewController) [self presentViewController:viewController]; return; } if (error) { if(completionHandler) completionHandler(error); } else { [self authenticationCompleted]; if(completionHandler) completionHandler(nil); } }; 

With a completion handler ...

 if(!error) { CCLOG(@"Adding invite handler."); [GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) { CCLOG(@"Invite recieved."); // Insert game-specific code here to clean up any game in progress. if (acceptedInvite) { GKMatchmakerViewController *mmvc = [[GameDataCache sharedData].match createWithInvite:acceptedInvite]; mmvc.matchmakerDelegate = [GameDataCache sharedData].match; } else if (playersToInvite) { GKMatchRequest *request = [[GKMatchRequest alloc] init]; request.minPlayers = 2; request.maxPlayers = 2; request.playersToInvite = playersToInvite; GKMatchmakerViewController *mmvc = [[GameDataCache sharedData].match createNewRequest:request cancelledBlock:nil]; mmvc.matchmakerDelegate = [GameDataCache sharedData].match; } }; } 

Pre iOS6 must call the authenticationWithCompletionHandler on the local server instead of setting the authenticateHandler property.

0
source

All Articles