Updating the leaderboard from the code
It is simple to send a new score to the leaderboard from the code. Follow these steps to send the number of coins collected to the leaderboard every time a game ends:
- In Xcode, open
GameSCNScene.swift
. - Add an import statement at the top so we can use the GameKit framework in this file:
import GameKit
- Add a new function in the
GameSCNScene
class namedupdateLeaderboard
, as shown here:func updateLeaderboard() { if GKLocalPlayer.localPlayer().isAuthenticated { // Create a new score object, with our leaderboard: let highScore = GKScore(leaderboardIdentifier: "highscore") // Set the score value to our coin score: highScore.value = Int64(self.score) // Report the score (wrap the score in an array) GKScore.report([highScore], withCompletionHandler: {(error : Error?) -> Void in // The error handler was...