验证本地播放器 - 游戏中心 - iOS6

时间:2012-10-04 01:35:04

标签: authentication ios6 game-center

我对Game Center的开发完全不熟悉。我在WWDC上观看了视频并查看了开发者网站。他们建议我为iOS 6输入这样的代码:

- (void) authenticateLocalPlayer
{
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
         if (viewController != nil)
         {
             [self showAuthenticationDialogWhenReasonable: viewController
         }
         else if (localPlayer.isAuthenticated)
         {
             [self authenticatedPlayer: localPlayer];
         }
         else
         {
             [self disableGameCenter];
         }
     }];
}

我已将其复制到app delegate.m文件中,但它不喜欢它,在[self showAuthenticationDialogWhenReasonable:viewController]后显示错误,如期待a]          } 在其他人中。

有人可以告诉我如何在iOS 6中验证游戏中心用户的身份吗?

3 个答案:

答案 0 :(得分:2)

要获得GameKit的介绍,可以从apple获得样本,例如: https://developer.apple.com/library/ios/#samplecode/GKLeaderboards/Introduction/Intro.html

在你的代码中你缺少结束“]”,但当然你需要的不仅仅是这个功能来连接到gameCenter。最好从其中一个样本开始。

答案 1 :(得分:1)

Apple已发布错误的代码,];代码末尾属于这一行的末尾[self showAuthenticationDialogWhenReasonable:viewController

这个代码不需要,因为这只是解释了authenticateLocalPlayer在Gamekit中的工作原理

答案 2 :(得分:0)

这是我所做的,而不必使用弃用的方法:

通过调用下面的函数在AppDelegate中立即设置身份验证处理程序(我把它放在一个单例辅助对象中)。此时,没有用于显示登录视图控制器的视图控制器,因此如果身份验证失败,并且处理程序为您提供了视图控制器,则只需将其保存。用户未登录时就是这种情况。

- (void)authenticateLocalUserNoViewController {
    NSLog(@"Trying to authenticate local user . . .");

    GKLocalPlayer *_localPlayer = [GKLocalPlayer localPlayer];
    __weak GKLocalPlayer *localPlayer = _localPlayer;  // Avoid retain cycle inside block
    __weak GCHelper *weakself = self;
    self.authenticationViewController = nil;

    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error)
    {
        if (viewController) {
            NSLog(@"User not logged in");
            weakself.authenticationViewController = viewController; // save it away
        } else if (localPlayer.authenticated) {
            [[GKLocalPlayer localPlayer] unregisterListener:self];
            [[GKLocalPlayer localPlayer] registerListener:self];
            NSLog(@"Local player %@ (%@) authenticated.  ID = %@",localPlayer.alias, localPlayer.displayName, localPlayer.playerID);
        } else {
            // Probably user cancelled the login dialog
            NSLog(@"Problem authenticating %@", [error localizedDescription]);
        }
    };

}

然后,一旦你的主屏幕加载,并且用户想要按下按钮启动在线游戏,就会出现你之前隐藏的登录视图控制器。我把它放在我帮助器类的另一个方法中。当用户登录时,它将触发原始认证块的执行,但viewcontroller参数将为nil。

-(BOOL) showGameCenterLoginController:(UIViewController *)presentingViewController {
    if (self.authenticationViewController) {
        [presentingViewController presentViewController:self.authenticationViewController animated:YES completion:^{
                }];
        return YES;
    } else {
        NSLog(@"Can't show game center view controller!");
        return  NO; // Show some other error dialog like "Game Center not available"
    }

}
相关问题