ios – authPlayerWithCompletionHandler不推荐使用,所以如何使用authenticateHandler

前端之家收集整理的这篇文章主要介绍了ios – authPlayerWithCompletionHandler不推荐使用,所以如何使用authenticateHandler前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
标题中有很多问题,authPlayerWithCompletionHandler已被弃用,那么如何使用authenticateHandler?

解决方法

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];
                }
            })];
        }
    }
}
原文链接:https://www.f2er.com/iOS/329330.html

猜你在找的iOS相关文章