iOS 6游戏中心authenticateHandler取消后无法登录 [英] iOS 6 Game Center authenticateHandler can't login after a cancel

查看:621
本文介绍了iOS 6游戏中心authenticateHandler取消后无法登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When using the authenticateHandler in iOS 6, game center won't present the login view if the user cancels it. I realize game center will auto lockout an app after 3 cancel attempts, but I'm talking about just 2 attempts. If they cancel the login, they have to leave the app and come back before game center will present the login even through the authenticateHandler is getting set again. Any ideas on how to handle this case in iOS 6?

使用旧的authenticateWithCompletionHandler方法时它可以正常工作:

It works fine when using the older authenticateWithCompletionHandler method:

#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_6_0
    GKLocalPlayer.localPlayer.authenticateHandler = authenticateLocalPlayerCompleteExtended;
#else
    [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:authenticateLocalPlayerComplete];
#endif

这对我的应用来说很重要的原因是它需要Game Center for多玩家。该应用程序尝试在启动时向游戏中心进行身份验证,但如果用户取消,我们不会再次启动它们,因此它们不会被唠叨。如果他们在选择多人游戏时没有登录,我们所做的就是显示游戏中心登录按钮。登录按钮通过调用authenticateWithCompletionHandler强制游戏中心登录(现在再次通过设置GKLocalPlayer.localPlayer.authenticateHandler)。

The reason this is important for my app is that it requires Game Center for multi-player. The app tries to authenticate to game center on launch, but if the user cancels we don't ask them at launch again so they won't get nagged. What we do is show a Game Center Login button if they aren't logged in when they select multi-player. The login button forces a game center login by calling authenticateWithCompletionHandler (and now by setting GKLocalPlayer.localPlayer.authenticateHandler again).

推荐答案

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

Better use runtime checks (instancesRespondToSelector:) instead of preprocessor #if statements, so that you can use recommended methods where they are available and depreciated ones elsewhere. I actually found I need to distinguish three cases before setting the invite handler, as the authentication handler might also get called with a 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中也是新的,并且避免了另一轮游戏中心视图控制器:

Yet more cases must be distinguished within the invite handler, as matchForInvite:: is new in iOS6 as well and avoids yet another round through game center view controllers:

-(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
            }
        };
    }
}

如果有帮助,请告诉我。

Let me know if this helps.

这篇关于iOS 6游戏中心authenticateHandler取消后无法登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆