2

我想通过 GameCenter 登录 API 登录我的应用程序。

是否可以 ?

苹果游戏中心登录 API 是公开的吗?

4

2 回答 2

1

你肯定可以做到的。没有直接使用的游戏中心 API。您可以显示游戏中心验证屏幕,验证后,您可以继续。

于 2012-11-22T11:18:06.303 回答
1

如果您使用的是 iOS 6,请参阅 GKLocalPlayer 的文档。您将看到您为 localPlayer 的“authenticateHandler”属性分配了一个块。当您分配它时,如果玩家尚未登录 Game Center,则该块的参数之一 (UIViewController *viewController) 将填充一个视图控制器的地址,该地址将显示常规的 Apple Game Center 登录屏幕。获得该地址后,您执行 presentViewController:viewController 并且用户会看到正常的 Apple 登录屏幕。当用户完成与它的交互时,您会收到对“gameCenterViewControllerDidFinish”的回调。您提供的块运行不止一次,这使得该过程很难遵循,但它确实有效。对于它的价值,我将在下面发布我正在使用的似乎有效的方法。它假定 iOS5 或 iOS6。这对于 5 之前的任何东西都不好。OS6 是在 iOS6 上返回 YES 的方法,否则返回 NO。这不是为公众消费而编写的,因此请原谅所有调试内容和其中无法解释的内容。

-(void) authenticateLocalPlayer {
ANNOUNCE
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
_lastError = nil;

        //iOS 6
if ( [self os6] ) {
    localPlayer.authenticateHandler = ^(UIViewController *loginVC, NSError *error) {
        NSLog(@"in authenticateHandler 1");
        [self setLastError:error];
            //... resume application responses
        [[CCDirector sharedDirector] resume];   //if not paused does nothing 
        if ( [GKLocalPlayer localPlayer].authenticated) {
            NSLog(@"in authenticateHandler 2 - local player is authenticated");
        } else if (loginVC) {
            NSLog(@"in authenticateHandler 3 - local player is not authenticated, will present VC");
                //... pause applications responses
            [[CCDirector sharedDirector] pause];
            [self presentViewController:loginVC];
        } else {
            NSLog(@"in authenticateHandler 4 - local player is NOT authenticated, no VC returned");
        }
        NSLog(@"authenticateHandler error: %@", error.localizedDescription);
    };

        //iOS 5
} else {
    if ( [GKLocalPlayer localPlayer].authenticated == NO ) {
            //no completion handler because we're relying on NSNotificationCenter
        [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:nil];
        NSLog(@"local player authentication requested");
    } else {
        NSLog(@"local player was already authenticated");
    }
}

}

于 2012-11-23T16:33:29.233 回答