Login in iPhone App via GameKit

半世苍凉 提交于 2019-12-06 13:05:59

If you're using iOS 6, see the documentation for GKLocalPlayer. You'll see that you assign a block to the 'authenticateHandler' property of localPlayer. When you assign it, if the player isn't already logged into Game Center, one of the arguments to the block (UIViewController *viewController) gets filled in with the address of a view controller that will present the regular Apple Game Center login screen. After you get that address you do presentViewController:viewController and the user sees the normal Apple login screen. When the user finishes interacting with it you get a call back to 'gameCenterViewControllerDidFinish'. The block you provide runs more than once, which makes the process pretty hard to follow, but it works. For what it's worth I'll post below a method I'm using that seems to work. It assumes either iOS5 or iOS6. It isn't be good for anything earlier than 5. OS6 is method that returns YES on iOS6 and NO otherwise. This wasn't written for public consumption so please excuse all the debugging stuff and the unexplained stuff in it.

-(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");
    }
}

}

You can do that surely. there is no gamecenter API for direct use. you can show the gamecenter authentication screen and after authentication, you can proceed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!