参考:http://stackoverflow.com/questions/12870041/ios-6-game-center-authenticatehandler-cant-login-after-a-cancel
static BOOL g_isGameCenterInit = NO;
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion]
compare:v options:NSNumericSearch] == NSOrderedAscending)
- (void)authenticateLocalUser:(id) sender
{
if (g_isGameCenterInit) {
[[NSNotificationCenter defaultCenter] postNotificationName:UIApplicationWillEnterForegroundNotification object:[UIApplication sharedApplication]];
return;
}
g_isGameCenterInit = YES;
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
// 已登录
if (localPlayer.isAuthenticated) {
[self sendOk];
} else {
if (SYSTEM_VERSION_LESS_THAN(@"6.0"))
{
// ios 5.x and below
[localPlayer authenticateWithCompletionHandler:^(NSError *error)
{
if (localPlayer.isAuthenticated)
{
[self sendOk];
}
}];
}
else
{
// ios 6.0 and above
[localPlayer setAuthenticateHandler:(^(UIViewController* viewController, NSError *error) {
[[CCDirector sharedDirector] resume];
if (error && error.code == GKErrorCancelled) {
NSLog(@"Game Center code:%d %@", error.code, [error debugDescription]);
[self sendFaild];
return ;
}
if (localPlayer.isAuthenticated) {
[self sendOk];
}
else if (viewController) {
[[CCDirector sharedDirector] pause];
AppDelegate* delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
RootViewController* root = (RootViewController*)delegate.viewController;
[root presentViewController:viewController animated:YES completion:nil];
}
})];
}
}
}