I am trying to invite nearby players to the match, but the invitation is either never sent or never received.
GKMatchMaker startBrowsingForNearbyPlayersWithHandler works and returns nearby players who are on the same Wi-Fi, but then I use findMatchForRequest and it returns the match without any players, and the players I'm trying to invite never receive an invitation. Here is my code.
I start by local player authentication:
GKLocalPlayer.localPlayer.authenticateHandler= ^(UIViewController *controller, NSError *error) { if (error) { NSLog(@"%s:: Error authenticating: %@", __PRETTY_FUNCTION__, error.localizedDescription); return; } if(controller) { // User has not yet authenticated [pViewController presentViewController:controller animated:YES completion:^(void) { [self lookForNearbyPlayers]; }]; return; } [self lookForNearbyPlayers]; }; -(void)lookForNearbyPlayers { if(!GKLocalPlayer.localPlayer.authenticated) { NSLog(@"%s:: User not authenticated", __PRETTY_FUNCTION__); return; }
I register the view controller as a delegate to the GKLocalPlayerListener:
[GKLocalPlayer.localPlayer registerListener:self]; // self is a view controller. // This works. My test local player which is a second device and appleID I setup shows up when this handler is called. [GKMatchmaker.sharedMatchmaker startBrowsingForNearbyPlayersWithHandler:^(GKPlayer *player, BOOL reachable) { NSArray * paPlayers= [NSArray arrayWithObject:player]; _pMatchRequest= [[GKMatchRequest alloc] init]; _pMatchRequest.minPlayers= 2; _pMatchRequest.maxPlayers= 4; _pMatchRequest.recipients = paPlayers; _pMatchRequest.inviteMessage = @"Join our match!"; _pMatchRequest.recipientResponseHandler = ^(GKPlayer *player, GKInviteeResponse response) { // This is never called. NSLog((response == GKInviteeResponseAccepted) ? @"Player %@ Accepted" : @"Player %@ Declined", player.alias); }; // This returns with a match without any players. [GKMatchmaker.sharedMatchmaker findMatchForRequest:_pMatchRequest withCompletionHandler:^(GKMatch *match, NSError *error) { if(error) { NSLog(@"%s:: %@", __PRETTY_FUNCTION__, error.localizedDescription); return; } else if(match != nil) { _pMatch= match; match.delegate = self; NSLog(@"players count= %lu", (unsigned long)_pMatch.players.count); // Always returns 0 } }]; } }
I have delegation methods for setting up GKLocalPlayerListener, but they are never called:
- (void)player:(GKPlayer *)player didRequestMatchWithRecipients:(NSArray<GKPlayer *> *)recipientPlayers { NSLog(@"%s", __PRETTY_FUNCTION__); } - (void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite { NSLog(@"%s", __PRETTY_FUNCTION__); }
Does anyone know how to make this work without GKMatchmakerViewController and for iOS9? The only examples I can find are the deprecated -inviteHandler method.
ios objective-c ios9 gamekit game-center
Mark
source share