根据 Apple 的 Game Center 编程指南,此代码设置了一个身份验证处理程序。如果您在游戏开始时运行此命令,第一次运行它时,如果用户尚未登录,它将提示用户登录。
- (void)authenticateLocalPlayer {
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
if (viewController != nil) {
NSLog(@"Player not authenticated.");
} else if (localPlayer.isAuthenticated) {
NSLog(@"Authentication successful.");
} else {
NSLog(@"Authentication failed. Error: %@.",error);
}
};
}
假设用户还没有登录,取消认证界面即可正常玩游戏。
我的游戏中有一个用于进行多人比赛的按钮。如果用户按下该按钮,它将尝试通过显示一个信息来搜索其他玩家GKMatchmakerViewController
实例。
由于玩家未登录,因此玩家实际上会收到一个错误对话框,指出他们未登录。该对话框只有一个“确定”按钮,可将其关闭。
如果玩家坚持按此按钮,则会出现相同的对话框。
然而,这是一种奇怪的行为。更合理的是,如果玩家想玩多人比赛但尚未登录,游戏会提示用户登录。
上面的代码设置了一个处理程序,所以它确实不是我想要的。但是,我下了一个断点并注意到viewController
is a GKHostedAuthenticateViewController
实例。我想也许我可以创建该类的一个实例并呈现它,这在技术上应该相当于提示用户登录。
但是,当我编写该类时,Xcode 似乎无法识别该类。我的印象是我不被允许这样做。
如何手动提示用户登录Game Center?
您可以首先通过读取 GKLocalPlayer 对象来检查玩家是否已通过身份验证。
如果没有经过身份验证的用户,您可以打开游戏中心应用程序。这种方法的缺点是,用户通过游戏中心应用程序进行身份验证后,他仍然在游戏中心应用程序中,并且必须“切换回”您的应用程序。当他切换回来时,您在代码中定义的身份验证处理程序将被触发。
-(void)clickedOnStartGame
{
if (_signedIn)
{
//Do what you need to.
}
else if (!_signedIn)
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Game Center"
message:@"If Game Center is disabled try logging in through the Game Center app"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:@"Open Game Center", nil];
[alertView show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"gamecenter:"]];
}
}
编辑:请注意,在 Apple 的文档中,他们说您不应提示用户再次登录,或显示登录提示。自动化方式(您的代码已经具有)应该是可接受的方式。显示我上面描述的警报视图只是帮助用户登录游戏中心,因为您不应该强制应用程序显示对话。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)