Author Topic: How to report a score to Game Center in Swift  (Read 3989 times)

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
How to report a score to Game Center in Swift
« on: August 16, 2015, 08:34:22 PM »
This method demonstrates how to report a score to Game Center:

Code: [Select]
    func submitScore(score: Int, leaderboardID: String) {
       
        if GKLocalPlayer.localPlayer().authenticated {
           
            // Create and initialize a GKScore object
            let gkScore = GKScore(leaderboardIdentifier: leaderboardID)
            gkScore.value = Int64(score)
           
            // Report the score
            GKScore.reportScores([gkScore], withCompletionHandler: { (error: NSError!) -> Void in
                if error != nil {
                    println("Error: \(error.localizedDescription)")
                }
                else {
                    println("Score Reported")
                }
            })
        }
    }

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:22:09 AM by kjmcneish »