设置AuthenticateHandler无法通过Game Center进行身份验证

时间:2013-04-10 05:05:24

标签: iphone ios objective-c ios6 game-center

我是iOS的新手,正在尝试编写一款有趣的回合制iOS游戏。我正在尝试立即对本地用户进行身份验证,并且在构建此代码时(尽管有保留周期警告),它始终无法通过GameCenter进行身份验证。

// Authenticate the local user.
- (void)authenticateLocalUser
{
if (!gameCenterAvailable) return;

GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
[localPlayer setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error)
{
    //[localPlayer authenticateWithCompletionHandler:^(NSError *error) { OLD CODE!
    if (localPlayer.isAuthenticated)
    {
        // Do some stuff.
    }
    else
    {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"NOT AUTHORISED"
                                  message:@"YOUR'RE NOT LOGGED INTO GC."
                                  delegate:self
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}
)];

}

我的旧代码仍然有效,但在iOS6中已弃用:

NSLog(@"Authenticating local user...");
if ([GKLocalPlayer localPlayer].authenticated == NO)
{
    [[GKLocalPlayer localPlayer]
     authenticateWithCompletionHandler:nil];
}
else
{
    NSLog(@"Already authenticated!");
}

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

当播放器未经过身份验证时,该处理程序会传递UIViewController。如果收到视图控制器,则您有责任显示它。

localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
    if (viewController != nil) {
        [self presentViewController:viewController animated:YES completion:nil];
    } else if (instance.player.isAuthenticated) {
        //Your handler will be called a second time once the user authenticates GC
        //using the view controller above ^^^^^
    } else if (error != nil) {
        //If all else fails, you'll have an error. Handle it
    }
};

答案 1 :(得分:0)

我发现在闭包之前/之前设置的localPlayer未经过身份验证,而如果我检索了一个新的GKLocalPlayer.localPlayer()对象,则会对其进行身份验证。

func authenticatePlayer() {
  local_player.authenticateHandler = {(view_controller, error) -> Void in

    // Note that self.local_player.authenticated and 
    //  GKLocalPlayer.localPlayer().authenticated give different
    //  results

    self.local_player = GKLocalPlayer.localPlayer()
    if (self.local_player.authenticated) {
      // Successfully authenticated
    } else if (view_controller != nil) {
      // Not yet authenticated
    } else {
      // authentication failed
    }
  }
}

在这种情况下,local_player在周围的类中设置。

注意:我实际上并不确定为什么这些会产生不同的结果。