如何通过GameCenter验证玩家

时间:2014-07-13 15:26:09

标签: ios game-center

我需要通过游戏中心验证玩家。我设法做到了,但它停止了工作。现在我总是得到“viewController = nil”,当检查localPlayer是否经过身份验证时,它总是返回false。

这就是我正在做的事情:

-(void)authenticateLocalPlayer{
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
        if (viewController != nil) {
            [_spriteViewController presentViewController:viewController animated:YES completion:nil];
        } else {
            if ([GKLocalPlayer localPlayer].authenticated) {
                NSLog(@"authenticated");
                _gameCenterEnabled = YES;

                // Get the default leaderboard identifier.
                [[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) {

                    if (error != nil) {
                        NSLog(@"%@", [error localizedDescription]);
                    } else {
                        _leaderboardIdentifier = leaderboardIdentifier;
                    }
                }];
            } else {
                NSLog(@"not authenticated"); // This is always triggered
                _gameCenterEnabled = NO;
            }
        }
    };
}

我做错了吗?我应该做什么/首先检查这个是为了工作?

1 个答案:

答案 0 :(得分:0)

如果您在同一设备上测试过多次,则首先重置您的手机设置。请参阅this link

然后这段代码会帮助你。

       if ([GKLocalPlayer localPlayer].authenticated == NO) {
            GKLocalPlayer* localPlayer = [GKLocalPlayer localPlayer];

            localPlayer.authenticateHandler = ^(UIViewController *gcvc,NSError *error) {

                if(gcvc) {

                    [self presentViewController:gcvc animated:YES completion:nil];
                }
                else {
                    _gameCenterEnabled = NO;

                }
            };
        }

        else if ([GKLocalPlayer localPlayer].authenticated == YES){
            _gameCenterEnabled = YES;


        }

对于排行榜标识符,您可以在已从iTunes中获取的代码中首先静态定义。它工作正常。但在此重置之前,游戏中心所需的所有设置都好像多次游戏中心身份验证失败,然后游戏中心禁用该应用程序。我们必须重置所有设置。如果你想尝试然后尝试在iPhone模拟器。重置iphone模拟器并运行此代码。它有效。