10

标题中的问题非常多,authPlayerWithCompletionHandler 已弃用,那么我该如何使用 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 回答