Authentication 验证本地玩家-游戏中心-iOS6
我对游戏中心的开发完全陌生。我在WWDC中观看了视频,并查看了开发者网站。他们建议我为iOS 6输入如下代码:Authentication 验证本地玩家-游戏中心-iOS6,authentication,ios6,game-center,Authentication,Ios6,Game Center,我对游戏中心的开发完全陌生。我在WWDC中观看了视频,并查看了开发者网站。他们建议我为iOS 6输入如下代码: - (void) authenticateLocalPlayer { GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
- (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]之后需要一个]
}除其他外
有谁能告诉我如何在iOS 6中验证game center用户的身份吗?要了解GameKit的介绍,可以从apple获得一些示例,例如:
在您的代码中,您缺少结束“]”,但当然,连接到gameCenter需要的不仅仅是此函数。最好从其中一个示例开始。苹果发布了错误的代码,];在代码的末尾,该代码属于这一行的末尾[self-showAuthenticationDialogWhenReasonable:viewController]
不需要这段代码,因为这只是解释了authenticateLocalPlayer方法在Gamekit中的工作原理 通过调用下面的函数,立即在AppDelegate中设置身份验证处理程序(我将其放在singleton helper对象中)。此时,没有显示登录视图控制器的视图控制器,因此如果身份验证失败,并且处理程序给您一个视图控制器,只需将其保存。这是用户未登录时的情况
- (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参数将零
-(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"
}
}