IOS Game Center GKLocalPlayerListener

后端 未结 3 1054
梦毁少年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条回答
  •  -上瘾入骨i
    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.

提交回复
热议问题