Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting score to gamecenter leaderboards

I am creating a game using swift on apple's Xcode 6 beta 6, and trying to add the high score of my game to gamecenter leader boards. I have created the leader boards in gamecenter.

So, how do I add my high score, which I saved as a NSUserDefault, to my gamecenter leader boards?

I tried using :

GKScore.reportScore([highScore], withCompletionHandler: nil)

but it just crashes. The initLeaderboard func has been deprecated in ios 8 so I'm not sure what to do.

like image 821
Oliver Shi Avatar asked Aug 28 '14 01:08

Oliver Shi


People also ask

How do you integrate Game Center?

iOS Configurations Now you need to go to the iTunes Connect, Then select your app, then select features, then Game Center, now from here you can add an achievement or a leaderboard it's a very simple process.

How do you make a leaderboard in Swift?

From the app home page, select the horizontal tab “Services” and the vertical tab “Game Center” to access the Game Center Settings. Under “Leaderboard”, click the + to create a new leaderboard. Select either a “Classic” or “Recurring” leaderboard and fill out the rest of the prompts.


1 Answers

First you have to create the GKScore object. Then you set the gkScore.value. Finally, you report the score.

// if player is logged in to GC, then report the score
if GKLocalPlayer.localPlayer().authenticated {
    let gkScore = GKScore(leaderboardIdentifier: "leaderBoardID")
    gkScore.value = score
    GKScore.reportScores([gkScore], withCompletionHandler: ( { (error: NSError!) -> Void in
        if (error != nil) {
            // handle error
            println("Error: " + error.localizedDescription);
        } else {
            println("Score reported: \(gkScore.value)")
        }
    }))
}
like image 199
Ron Fessler Avatar answered Sep 19 '22 17:09

Ron Fessler