首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >取消后,iOS 6游戏中心authenticateHandler无法登录

取消后,iOS 6游戏中心authenticateHandler无法登录
EN

Stack Overflow用户
提问于 2012-10-13 12:47:27
回答 2查看 8.7K关注 0票数 26

在iOS 6中使用authenticateHandler时,如果用户取消,游戏中心将不会显示登录视图。我意识到游戏中心会在3次取消尝试后自动锁定应用程序,但我说的是2次尝试。如果他们取消登录,他们必须离开应用程序并在游戏中心显示登录之前返回,即使authenticateHandler正在重新设置。对于如何在iOS 6中处理这种情况,您有什么想法吗?

在使用旧的authenticateWithCompletionHandler方法时,它工作得很好:

代码语言:javascript
复制
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_6_0
    GKLocalPlayer.localPlayer.authenticateHandler = authenticateLocalPlayerCompleteExtended;
#else
    [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:authenticateLocalPlayerComplete];
#endif

这对我的应用程序很重要的原因是它需要多人游戏中心。该应用程序试图在发布时验证到游戏中心,但如果用户取消,我们不会在发布时再次询问他们,这样他们就不会被骚扰。当他们选择多玩家时,如果他们没有登录,我们所做的是显示一个游戏中心登录按钮。login按钮通过调用authenticateWithCompletionHandler (现在再次设置GKLocalPlayer.localPlayer.authenticateHandler )强制游戏中心登录。

EN

回答 2

Stack Overflow用户

发布于 2013-05-07 16:38:01

最好使用运行时检查(instancesRespondToSelector:)而不是预处理器#if语句,这样您就可以在推荐的方法可用的地方使用推荐的方法,并在其他地方使用推荐的方法。实际上,我发现在设置invite处理程序之前需要区分三种情况,因为身份验证处理程序也可能会被nil视图控制器调用:

代码语言:javascript
复制
 -(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];
         }];
     }
 }

在invite处理程序中必须区分更多的情况,因为matchForInvite::在iOS6中也是新的,并且避免了通过游戏中心视图控制器的另一轮:

代码语言:javascript
复制
-(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
            }
        };
    }
}

如果这有帮助,请告诉我。

票数 2
EN

Stack Overflow用户

发布于 2013-05-29 15:32:43

我认为这在iOS 6.0中是不可能的。在发布之前删除的早期SDK版本中,有一些API调用可以做到这一点。

在WWDC2012视频: Session 516 -将游戏与游戏中心集成8:30中,他们实际上显示了调用authenticate方法的代码:

代码语言:javascript
复制
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticationHandler = //handle the callback...
[localPlayer authenticate];

此方法现在是私有API,但您可以通过调用以下命令来查看它的实际效果:

代码语言:javascript
复制
[[GKLocalPlayer localPlayer] performSelector:@selector(_authenticate)];

它可以做你想做的事情,但不能使用,因为它现在是私有的。

您还可以通过发布UIApplicationWillEnterForegroundNotification通知来触发身份验证过程:

代码语言:javascript
复制
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationWillEnterForegroundNotification object:[UIApplication sharedApplication]];

我认为用实时代码来做这件事是不可取的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12870041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档