基本上在ViewController
' viewdidload
方法下,我把:
[self authenticateLocalUser];
authenticateLocalUser
方法是:
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
if (localPlayer.isAuthenticated == NO)
{
NSLog(@"FAILED");
}
为什么不进行身份验证?不应该GKLocalPlayer
的类方法自动进行身份验证吗?此外,如果它确实进行了身份验证,我是否应该接受 _ _"在顶端?我根本没有得到这个横幅。我事先需要做些什么吗?
答案 0 :(得分:0)
您需要安装authenticateHandler
。 Apple的Game Center编程指南中的section给出了一个逐字的例子。代码就像这样开始(参见该部分链接的完整源代码):
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = // ...
答案 1 :(得分:0)
在iOS6及以上版本中,您需要使用setAuthenticateHandler api呈现Game Center登录视图。
这是代码:
#define CHECK_IOS_LESS_THAN(version) ([[[UIDevice currentDevice] systemVersion] \
compare:version options:NSNumericSearch] == NSOrderedAscending)
- (void) authenticateLocalUser
{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
if (CHECK_IOS_LESS_THAN(@"6.0"))
{
// ios 5.x and below
[localPlayer authenticateWithCompletionHandler:^(NSError *error)
{
[self checkLocalPlayer];
}];
}
else
{
// ios 6.0 and above
[localPlayer setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error)
{
if (!error && viewcontroller)
{
[self.navController
presentViewController:viewcontroller animated:YES completion:nil];
}
else
{
[self checkLocalPlayer];
}
})];
}
}
- (void)checkLocalPlayer
{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
if (localPlayer.isAuthenticated)
{
/* Perform additional tasks for the authenticated player here */
[self processGameCenterLoginSuccess];
}
else
{
/* Perform additional tasks for the non-authenticated player here */
[self processGameCenterLoginFaild];
}
}
添加此代码后,转到设备设置并重置: 设备:设置 - >常规 - >重置 - >重置所有设置 模拟器:设置 - >常规 - >重置 - >重置位置和隐私