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.
aronspring
source share