GKLocalPlayerListener protocol is not called
Asked Answered
B

1

7

As the apple guidelines said; I've implemented the GKLocalPlayerListener protocol in my game center class and add the local player as listener as soon as he's authenticated:

func authenticationChanged() {
  if (GKLocalPlayer.localPlayer().authenticated && !self.userAutenticated) {
    println("Authentication changed: player authenticated.")
    userAutenticated = true
    GKLocalPlayer.localPlayer().unregisterAllListeners()
    GKLocalPlayer.localPlayer().registerListener(self)
  } else if (GKLocalPlayer.localPlayer().authenticated && self.userAutenticated) {
    println("Authentication changed: player not authenticated.")
    userAutenticated = false
  }
}

Protocol implementation:

// MARK: - GKLocalPlayerListener
func player(player: GKPlayer!, didAcceptInvite invite: GKInvite!) {
  println("Did accept invite")
}
func player(player: GKPlayer!, didRequestMatchWithRecipients recipientPlayers: [AnyObject]!) {
  println("Did request matchmaking")
}

None of this 2 methods it's called when I try to invite a friend and I also didn't receive any kind of notifications. I've tried to test the game in release mode but I've got the same result. I must say the the normal matchmaking works correctly, I'm able to find player to play with without any problems.

EDIT:

If i test test it from 2 devices, the notification will be received but if i tap on the notification, the app will be open and no delegate will be called. If i close the app and restart it again, then the GKLocalPlayerListener it's called.

What's wrong??

Barnhill answered 6/8, 2015 at 6:58 Comment(2)
You probably only need to call unregisterAllListeners if authentication is changed to false.Meshwork
No, that didn't solve the problem. The user it's correctly authenticated and the delegate don't workBarnhill
M
1

I assume that, when you say "the normal matchmaking works" that you have presented a matchmakerviewcontroller:

-(IBAction)setupMatch:(id)sender{
GKMatchmakerViewController *matchViewController = [[GKMatchmakerViewController alloc] initWithMatchRequest:matchRequest];
matchViewController.matchmakerDelegate = self;
[self presentViewController:matchViewController animated:YES completion:nil];}

Then when the players are found in the matchmakerviewcontroller didFindMatch will be called:

-(void)matchmakerViewController:(GKMatchmakerViewController *)viewController didFindMatch:(GKMatch *)match{
//Called when GameCenter completes the matchmaking process
match.delegate = (id)self;   //etc. lots of your own code here.

didAcceptinvite is only called on the device of the recipient of an invite after they accept the invite:

-(void)player:(GKPlayer *)player didAcceptInvite:(GKInvite *)invite{
//Called on the accepting device when the invitation is accepted
GKMatchmakerViewController *mmvc = [[GKMatchmakerViewController alloc] initWithInvite:invite];
mmvc.matchmakerDelegate = self;
[self presentViewController:mmvc animated:YES completion:nil];

}

That presents a matchmakerviewcontroller to your friend. There is nothing for them to do, the vc makes the connection and then dismisses itself. The vc on the senders device dismisses at the same time.

Then didFindMatch is called on both devices and away you go.

I am not convinced didrequestMatchWithRecipients is ever called and it seems redundant when didFindMatch and didAcceptInvite deal with the game start at both ends.

I found this video from WWDC 2012 really helpful: WWDC 2012 Christy Warren

Melodramatize answered 2/3, 2016 at 15:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.