IOS Game Center GKLocalPlayerListener

后端 未结 3 1109
梦毁少年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.

提交回复
热议问题