Reputation: 1530
I'm simply trying to log a high score of a single game mode in a game I'm making. I have a leaderboard setup in Game Center on iTunes Connect.
So, my question is, how do I integrate this into my game? I've seen other solutions but can't seem to figure out how they fit into my project.
Thanks!
Upvotes: 0
Views: 347
Reputation: 133
GKGameCenterControllerDelegate
Create a local player
var localPlayer: GKLocalPlayer = GKLocalPlayer.localPlayer()
Authenticate the player in the viewDidLoad
with the game center and present the authentication success
localPlayer.authenticateHandler = {(ViewController, error) -> Void in
if((ViewController) != nil) {
self.presentViewController(ViewController, animated: true, completion: nil)
}
}
Report your score from anywhere in your game
if (GKLocalPlayer.localPlayer().authenticated) {
let gkScore = GKScore(leaderboardIdentifier: "YOUR-LEADERBOARD-ID")
gkScore.value = Int64(YOUR-SCORE)
GKScore.reportScores([gkScore], withCompletionHandler: ( { (error: NSError!) -> Void in
if (error != nil) {
// handle error
println("Error: " + error.localizedDescription);
} else {
println("Score reported: \(gkScore.value)")
}
}))
}
Upvotes: 1