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 を渡します。View Controller が受信した場合、それを表示するのはあなたの責任です。

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 に答える