在iOS 6 中使用authenticateHandler 时,如果用户取消登录,游戏中心将不会显示登录 View 。我意识到游戏中心会在 3 次取消尝试后自动锁定应用程序,但我说的只是 2 次尝试。如果他们取消登录,他们必须离开应用程序并在游戏中心显示登录之前返回,即使再次设置了 authenticateHandler 也是如此。关于如何在 iOS 6 中处理这种情况的任何想法?
使用旧的authenticateWithCompletionHandler 方法时它工作正常:
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_6_0
GKLocalPlayer.localPlayer.authenticateHandler = authenticateLocalPlayerCompleteExtended;
#else
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:authenticateLocalPlayerComplete];
#endif
这对我的应用程序很重要的原因是它需要多人游戏的游戏中心。该应用程序尝试在启动时向游戏中心进行身份验证,但如果用户取消,我们不会在启动时再次询问他们,因此他们不会被打扰。我们所做的是如果他们在选择多人游戏时未登录,则显示一个游戏中心登录按钮。登录按钮通过调用authenticateWithCompletionHandler(现在通过再次设置GKLocalPlayer.localPlayer.authenticateHandler)强制登录游戏中心。
最佳答案
更好地使用运行时检查 (instancesRespondToSelector:) 而不是预处理器 #if 语句,以便您可以使用推荐的方法在其他地方可用和折旧的方法。我实际上发现在设置邀请处理程序之前我需要区分三种情况,因为身份验证处理程序也可能被一个 nil View Controller 调用:
-(void)authenticateLocalPlayer
{
if ([[GKLocalPlayer class] instancesRespondToSelector:@selector(setAuthenticateHandler:)]) {
[[GKLocalPlayer localPlayer] setAuthenticateHandler:^(UIViewController *gameCenterLoginViewController, NSError *error) {
if (gameCenterLoginViewController) {
[self.presentedViewController presentViewController:gameCenterLoginViewController
animated:YES
completion:^{
[self setInviteHandlerIfAuthenticated];
}];
} else {
[self setInviteHandlerIfAuthenticated];
}
}];
} else { // alternative for iOS < 6
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
[self setInviteHandlerIfAuthenticated];
}];
}
}
在邀请处理程序中必须区分更多情况,因为 matchForInvite:: 在 iOS6 中也是新的,并且避免了通过游戏中心 View Controller 的另一轮:
-(void)setInviteHandlerIfAuthenticated
{
if ([GKLocalPlayer localPlayer].isAuthenticated) {
[GKMatchmaker sharedMatchmaker].inviteHandler = ^(GKInvite *acceptedInvite, NSArray *playersToInvite) {
if (acceptedInvite) {
if ([GKMatchmaker instancesRespondToSelector:@selector(matchForInvite:completionHandler:)]) {
[self showInfoAnimating:YES completion:NULL];
[[GKMatchmaker sharedMatchmaker] matchForInvite:acceptedInvite
completionHandler:^(GKMatch *match, NSError *error) {
// ... handle invited match
}];
} else {
// alternative for iOS < 6
GKMatchmakerViewController *mmvc = [[[GKMatchmakerViewController alloc] initWithInvite:acceptedInvite] autorelease];
mmvc.matchmakerDelegate = self;
// ... present mmvc appropriately
// ... handle invited match found in delegate method matchmakerViewController:didFindMatch:
}
} else if (playersToInvite) {
// ... handle match initiated through game center
}
};
}
}
如果这有帮助,请告诉我。
关于ios6 - iOS 6 Game Center authenticationHandler 取消后无法登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12870041/