1

我是 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!");
}

有什么建议么?

4

2 回答 2

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
    }
};
于 2013-04-10T20:45:39.453 回答
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设置在周围类中。

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

于 2015-05-15T14:30:25.613 回答