在标题中有很多问题,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];
}
})];
}
}
}