Game Center integration with Sprite Kit?

允我心安 提交于 2019-12-06 03:34:30

you can authenticate like this

[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
            if (error == nil)
            {
                static_setEnable( true );

                NSLog(@" Authenticate local player complete");

            }
            else
            {
                static_setEnable( false );
                NSLog(@"Authenticate local player Error: %@", [error description]);
            }
        }];
    }
Huygamer

You can use "presentModalViewController" by using this code to access the root view controller

UIViewController *vc = self.view.window.rootViewController;
[vc presentViewController: gameCenterController animated: YES completion:nil];

Now you can access your ModelViewController anywhere include in SKScenes. I did it in my newest game and it worked well

Besides, I suggest you use the separate object to control game center like leaderboard and achievement so you can reuse it in your next game.

Here is an updated authenticate local player, but Ravindra's code also works.

- (void) authenticateLocalPlayer
{
    GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
        if (viewController != nil)
        {
            //showAuthenticationDialogWhenReasonable: is an example method name. Create your own method that displays an authentication view when appropriate for your app.
            //[self showAuthenticationDialogWhenReasonable: viewController];
        }
        else if (localPlayer.isAuthenticated)
        {
            //authenticatedPlayer: is an example method name. Create your own method that is called after the loacal player is authenticated.
            //[self authenticatedPlayer: localPlayer];
        }
        else
        {
            //[self disableGameCenter];
        }
    };
}

Swift 2.0

 func authenticateLocalPlayer() {

        let localPlayer = GKLocalPlayer.localPlayer()

        localPlayer.authenticateHandler = {  (viewController, error )  -> Void in

            if (viewController != nil) {

                let vc:UIViewController = self.view!.window!.rootViewController!
                vc.presentViewController(viewController!, animated: true, completion:nil)

            } else {


                print ("Authentication is \(GKLocalPlayer.localPlayer().authenticated) ")
                GlobalData.loggedIntoGC = true



                // do something based on the player being logged in.

GlobalData Swift File:

static var loggedIntoGC:Bool = false

Call Method in your scene where Game Center is being enabled: ie HUD or GameScene in the

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