Man*_*nOx 10 gamekit ios game-center ios6
标题中的问题,authPlayerWithCompletionHandler是不推荐使用的,那么我如何使用authenticateHandler呢?
12o*_*2on 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];
}
})];
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在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
{
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10469 次 |
| 最近记录: |