authPlayerWithCompletionHandler已弃用,因此如何使用authenticateHandler

时间:2012-09-13 02:51:52

标签: ios game-center gamekit ios6

标题中的问题,authPlayerWithCompletionHandler已弃用,那么如何使用authenticateHandler?

3 个答案:

答案 0 :(得分: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];
                }
            })];
        }
    }
}

答案 1 :(得分: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
{

}

答案 2 :(得分: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
        }
    })];
}