IOS Game Center GKLocalPlayerListener
Asked Answered
F

3

10

I was trying to implement an event listener in a turn based game so a player can receive when his turn is active or when he is invited by a friend. GKTurnBasedEventHandler is deprecated in IOS 7 and i read in the documentation that i should use GKLocalPlayerListener; but that's the extend of it. Is there someone who used it already, because there is no info anywhere.

This is what i tried before, and it does not work.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    [localPlayer authenticateWithCompletionHandler:^(NSError *error)
     {
         if (localPlayer.isAuthenticated)
         { 
             GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
             [localPlayer registerListener:self];
         }
     }];

    return YES;
}

-(void)handleInviteFromGameCenter:(NSArray *)playersToInvite
{
    NSLog(@"test");
}

- (void)player:(GKPlayer *)player receivedTurnEventForMatch:(GKTurnBasedMatch *)match didBecomeActive:(BOOL)didBecomeActive
{
    NSLog(@"test");
}
Fetishist answered 18/10, 2013 at 6:35 Comment(4)
have you figured out or found anything else on this? i am having trouble finding anything on using the GKLocalPlayerListener. the documentation and examples available still leverage deprecated APIs.Fourierism
Not yet, some other urgent stuff came up, but if i do i will post the solution.Fetishist
It works for me to some degree. The methods are analogous to the now deprecated ones, except that the current player is passed as well. However what I'm having problems with is figuring out when to de/reregister listeners e.g. when the app switches between bg/fg. This was also an issue for invite listeners in iOS 6.Triatomic
I'm getting the same sort of flakey behavior. Sometimes the listener gets the turn or saved turn event. Most of the time, nothing. I have to restart the game to get the match info sync'd up. Also, the information in Game Center is not displayed as current. Whose turn it is isn't right until I reload the game. I'm using the sandboxed game center.Bambino
T
2

Here is some code that I use in order to register GKLocalPlayerListener

__weak GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {
   if (viewController) {
         [authenticateFromViewController presentViewController:viewController animated:YES completion:^{
          [localPlayer registerListener:self];
          NSLog(@"Authenticated. Registering Turn Based Events listener");
        }];
  } else if (localPlayer.authenticated) {
         [localPlayer registerListener:self];
         NSLog(@"User Already Authenticated. Registering Turn Based Events listener");
  } else {
         NSLog(@"Unable to Authenticate with Game Center: %@", [error localizedDescription]);
  }
};

The documentation states that you should only register for an GKLocalPlayerEventListener once so you could improve this code by checking if you've already registered.

Note that authenticateWithCompletionHandler is deprecated in iOS 6 and they recommend setting the authenticateHandler property like I did above.

Telemetry answered 20/11, 2013 at 0:3 Comment(0)
A
1

I believe you were there. Just this time do a couple of things. Make sure you dont add multiple listeners also before you add a listener, just incase unregister all listeners.

I made sure I only did this once in my whole project, but I get the local player multiple times.

-(void) onLocalPlayerAuthChanged:(GKLocalPlayer*)authPlayer {

    [authPlayer unregisterAllListeners];
    [authPlayer registerListener:_Whatever_];

}
Absentminded answered 27/11, 2013 at 6:43 Comment(0)
M
1

I might be a little late, but hopefully it will help someone out there...

This is what I do. According to Apple's documentation I create [my] own method that displays an authentication view when appropriate for [my] app.

    - (void)authenticateLocalUser
    {
        if ([GKLocalPlayer localPlayer].authenticated == NO) {
            __weak typeof(self) weakSelf = self;
            __weak GKLocalPlayer *weakPlayer = [GKLocalPlayer localPlayer];

            weakPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) {
                if (viewController != nil) {
                    [weakSelf showAuthenticationDialogWhenReasonable:viewController];
                } else if (weakPlayer.isAuthenticated) {
                    // Player has been authenticated!
                    [weakPlayer registerListener:weakSelf];
                } else {
                    // Should disable Game Center?
                }
            };

        } else {
            // Already authenticated
            [[GKLocalPlayer localPlayer] registerListener:self];
        }
    }


    -(void)showAuthenticationDialogWhenReasonable:(UIViewController *)controller
    {
        [[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:controller animated:YES completion:nil];
    }

This code is inside a singleton helper class, it might be simplified if you have it on your own class.

Misfire answered 19/3, 2014 at 18:54 Comment(1)
Thank you very very much, it was pending from the last 15 hours for me, and simply delegating was mentioned everywhere, but the correct syntax wasn't available. And I really converted this to swift for my own purpose. #44889519Chervonets

© 2022 - 2024 — McMap. All rights reserved.