Swift - Integrate GameCenter to use leaderboards

╄→гoц情女王★ 提交于 2019-12-07 06:35:03

问题


I am making a game in Swift. I want to be able to post the users' score using GameCenter, so that scores from all my users' can be seen. However, I have spent the past day trying to figure out how to do this, but I haven't found any helpful instructions.

I am pretty new to iOS programming, and Swift, and of the very little amount of information on this subject, it's all written in Objective-C.

Can anyone help me integrate GameCenter into my app, so that I can post users scores to the leaderboards for people to see?

EDIT: I have already created a GameCenter leaderboard on iTunesConnect.

EDIT 2: I have tried following this tutorial: http://www.appcoda.com/ios-game-kit-framework/ and converting it to Swift. I have converted this:

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

    localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
        if (viewController != nil) {
            [self presentViewController:viewController animated:YES completion:nil];
        }
        else{
            if ([GKLocalPlayer localPlayer].authenticated) {
                _gameCenterEnabled = YES;

                // Get the default leaderboard identifier.
                [[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) {

                    if (error != nil) {
                        NSLog(@"%@", [error localizedDescription]);
                    }
                    else{
                        _leaderboardIdentifier = leaderboardIdentifier;
                    }
                }];
            }

            else {
                _gameCenterEnabled = NO;
            }
        }
    };
}

into this:

func authenticateLocalPlayer() {
    var localPlayer : GKLocalPlayer!
    localPlayer.authenticateHandler = {(viewController : MenuViewController!, error : NSError!) -> Void in
        if viewController != nil {
            self.presentViewController(viewController, animated: true, completion: nil)
        } else {
            if localPlayer.authenticated {
                self.gameCenterEnabled = true

                localPlayer.loadDefaultLeaderboardIdentifierWithCompletionHandler({ (leaderboardIdentifier : String!, error : NSError!) -> Void in
                    if error != nil {
                        println(error.localizedDescription)
                    } else {
                        self.leaderboardIdentifier = leaderboardIdentifier
                    }
                })

            } else {
                self.gameCenterEnabled = false
            }
        }
    }
}

but it crashes on this line:

localPlayer.authenticateHandler = {(viewController : UIViewController!, error : NSError!) -> Void in

The Error message is:

fatal error: unexpectedly found nil while unwrapping an Optional value

I can't believe how hard this is!


回答1:


Your specific issue has nothing to do with Game Center and is happening because you have the line var localPlayer : GKLocalPlayer!.

You're declaring an implicitly unwrapped optional and then using it right away. It's value is nil in the subsequent line when you try to set localPlayer.authenticateHandler.

Instead you should instantiate GKLocalPlayer like so:

var localPlayer = GKLocalPlayer()

Note that there are currently issues with Game Center and Swift. Your code is going to work but localPlayer.authenticated never gets set to true. This issue is tracked here:

http://www.openradar.me/17825348

Credit to: http://www.stuarticus.net/blog/2014/7/game-center-authentication-and-swift for filing the radar ticket.




回答2:


You can use that, I create a simple class for iOS game center in github Easy Class Game Center Swift https://github.com/DaRkD0G/Easy-Game-Center-Swift



来源:https://stackoverflow.com/questions/25291731/swift-integrate-gamecenter-to-use-leaderboards

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