IOS Game Center GKLocalPlayerListener

后端 未结 3 1058
梦毁少年i
梦毁少年i 2021-02-08 20:52

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 de

相关标签:
3条回答
  • 2021-02-08 21:08

    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.

    0 讨论(0)
  • 2021-02-08 21:08

    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.

    0 讨论(0)
  • 2021-02-08 21:23

    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_];
    
    }
    
    0 讨论(0)
提交回复
热议问题