GameCenter Leaderboards
suggest changePrerequisites:
- An Apple Developer account.
- Game Center leaderboards set up in App Store Connect (this walkthrough calls it iTunes Connect).
Setting up Game Center leaderboards:
- Sign in to App Store Connect.
- Go to My Apps. Create an app for your project, then go to Features.
- Click Game Center.
- Click the plus sign next to Leaderboards.
- Choose Single Leaderboard for the leaderboard type.
- Create a Leaderboard Reference Name for your reference.
- Create a Leaderboard ID for your app to use when reporting scores.
- Set the score format to Integer.
- Set score submission to Best Score.
- Click Add Language and fill in the entries.
Copy the LeaderboardID you made and head over to Xcode.
Working with Xcode
The Swift below is Swift 2 (localPlayer(), presentViewController, reportScores, and dismissViewControllerAnimated). Force-unwrapping view, window, rootViewController, and the sign-in controller crashes when any of them is nil. In authenticateLocalPlayer, the else branch also runs when authentication fails (error != nil). gameCenterViewControllerDidFinish calls gameCenterAchievements, which this page never declares.
There are four pieces:
- Import GameKit and adopt
GKGameCenterControllerDelegate. - Check whether the user is signed in to Game Center.
- Report scores.
- Show the leaderboards.
Import GameKit with import GameKit and adopt GKGameCenterControllerDelegate.
Check whether the user is signed in to Game Center:
func authenticateLocalPlayer() {
let localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = { (viewController, error) -> Void in
if viewController != nil {
//If the user is not signed in to GameCenter, we make them sign in
let vc:UIViewController = self.view!.window!.rootViewController!
vc.presentViewController(viewController!, animated: true, completion: nil)
} else {
//Do something here if you want
}
}
}
When the user has a new high score, report it with the function below.
The function holds two parameters.
identifier is the leaderboard ID from App Store Connect.
score is an Int, the user’s score to submit.
func saveHighScore(identifier:String, score:Int) {
if GKLocalPlayer.localPlayer().authenticated {
let scoreReporter = GKScore(leaderboardIdentifier: identifier)
scoreReporter.value = Int64(score)
let scoreArray:[GKScore] = [scoreReporter]
GKScore.reportScores(scoreArray, withCompletionHandler: {
error -> Void in
if error != nil {
print("Error")
} else {
}
})
}
}
If the user wants to view leaderboards, call the function below:
//This function will show GameCenter leaderboards and Achievements if you call this function.
func showGameCenter() {
let gameCenterViewController = GKGameCenterViewController()
gameCenterViewController.gameCenterDelegate = self
let vc:UIViewController = self.view!.window!.rootViewController!
vc.presentViewController(gameCenterViewController, animated: true, completion:nil)
}
//This function closes gameCenter after showing.
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController) {
gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
self.gameCenterAchievements.removeAll()
}