10

Questions pretty much in the title, authPlayerWithCompletionHandler is Deprecated, so how do I use authenticateHandler?

4

3 に答える 3

18

setAuthenticateHandler は iOS 6 で新しく追加されました。authenticateWithCompletionHandler は iOS 5 以前でも引き続き使用する必要があります。

また、presentViewController:animated:completion: の完了ハンドラーを提供する必要はありません。その完了ハンドラーは、完了時ではなく、ゲーム センター ビューが表示された直後に呼び出されるためです。

これが私の解決策です:

- iOS 4.3、iOS 5.1、iOS 6.0 シミュレーターのみでテストされており、実際のデバイスではテストされていません。

- これは、GameCenter API が利用可能であることを確認済みであることを前提としています。

- (void)checkLocalPlayer
{
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

    if (localPlayer.isAuthenticated)
    {
        /* Perform additional tasks for the authenticated player here */
    }
    else
    {
        /* Perform additional tasks for the non-authenticated player here */
    }
}

#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] \
compare:v options:NSNumericSearch] == NSOrderedAscending)

- (void)authenticateLocalPlayer
{
        GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

        if (SYSTEM_VERSION_LESS_THAN(@"6.0"))
        {
            // ios 5.x and below
            [localPlayer authenticateWithCompletionHandler:^(NSError *error)
             {
                 [self checkLocalPlayer];
             }];
        }
        else
        {
            // ios 6.0 and above
            [localPlayer setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error) {
                if (!error && viewcontroller)
                {
                    [[AppDelegate sharedDelegate].viewController
                    presentViewController:viewcontroller animated:YES completion:nil];
                }
                else
                {
                    [self checkLocalPlayer];
                }
            })];
        }
    }
}
于 2012-10-14T18:54:57.067 に答える
5

iOS 6以降でこのコードを使用しています。コンパイル エラーはなく、正常に動作しているようです。

#pragma
#pragma mark - Player Authentication
-(void)autheticatePlayer
{
    __weak typeof(self) weakSelf = self; // removes retain cycle error

    _localPlayer = [GKLocalPlayer localPlayer]; // localPlayer is the public GKLocalPlayer
    __weak GKLocalPlayer *weakPlayer = _localPlayer; // removes retain cycle error

    weakPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error)
    {
        if (viewController != nil)
        {
            [weakSelf showAuthenticationDialogWhenReasonable:viewController];
             }
             else if (weakPlayer.isAuthenticated)
             {
                 [weakSelf authenticatedPlayer:weakPlayer];
             }
             else
             {
                 [weakSelf disableGameCenter];
             }
             };
}

-(void)showAuthenticationDialogWhenReasonable:(UIViewController *)controller
{
    [[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:controller animated:YES completion:nil];
}

-(void)authenticatedPlayer:(GKLocalPlayer *)player
{
    player = _localPlayer;
}

-(void)disableGameCenter
{

}
于 2013-02-14T08:24:52.463 に答える
3

これは私が思いついたものです-それはうまくいくようです。私が何かを逃したと思うなら、自由に編集してください。

-(void)authenticatePlayer {
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    [localPlayer setAuthenticateHandler:(^(UIViewController* viewcontroller, NSError *error) {
        if (!error) {
            [self presentViewController:viewcontroller animated:YES completion:^{
                if (localPlayer.isAuthenticated)
                {
                    // your code if authenticated
                }
                else {
                    // your code if not authenticated
                }
            }];
        }
        else {
            // error handling code here
        }
    })];
}
于 2012-09-20T05:00:45.517 に答える