IOS Game Center GKLocalPlayerListener

只谈情不闲聊 提交于 2019-12-03 06:35:19

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.

tomJ

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_];

}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!