Author Topic: How to retrieve the local player's score from a leaderboard in Game Center  (Read 6827 times)

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
The following code retrieves a leaderboard and associated score for the current local player:

Code: [Select]
        if GKLocalPlayer.localPlayer().authenticated {
           
            // Initialize the leaderboard for the current local player
            var gkLeaderboard = GKLeaderboard(players: [GKLocalPlayer.localPlayer()])
            gkLeaderboard.identifier = leaderboardID
            gkLeaderboard.timeScope = GKLeaderboardTimeScope.AllTime
           
            // Load the scores
            gkLeaderboard.loadScoresWithCompletionHandler({ (scores, error) -> Void in
               
                // Get current score
                var currentScore: Int64 = 0
                if error == nil {
                    if scores.count > 0 {
                        currentScore = (scores[0] as! GKScore).value
                    }
                }

            })
        }

All the best!
Kevin McNeish
Author of Learn to Code in Swift: https://itunes.apple.com/us/book/learn-to-code-in-swift/id942956811?mt=11
Check out our Robot Tic-Tac-Toe app: https://itunes.apple.com/us/app/robot-tic-tac-toe/id990638517?mt=8
« Last Edit: November 15, 2015, 10:21:48 AM by kjmcneish »