Oliver Shi
Oliver Shi

Reputation: 148

Reporting score to Game Center Xcode 6

I am creating a game using swift on apple's Xcode 6 GM seed. I'm adding gamecenter leaderboards, and would like to report scores to the leaderboard. I have everything setup but the report score function. Here's my code:

func reportScores() {
    if GKLocalPlayer.localPlayer().authenticated == true{
        var highScore = userDefaults.integerForKey("myHighScore")
        var scoreReporter = GKScore(leaderboardIdentifier: "myLeaderboarID")
        scoreReporter.value = Int64(highScore)
        var scoreArray: [GKScore] = [scoreReporter]
        GKScore.reportScores([scoreReporter], withCompletionHandler: nil) {
        }

    }

}

I try this and an error appears saying the reportScore method is not convertible to $T2. Could somebody please tell me how to post my score to the Game Center leaderboards? Thank you!

Upvotes: 1

Views: 1736

Answers (2)

henrik-dmg
henrik-dmg

Reputation: 1493

In Swift 2 you can do:

func reportScores() {
    var gameScoreReporter = GKScore(leaderboardIdentifier: "yourLeaderboardID")

    gameScoreReporter.value = Int64(easyHighscore)

    var scoreArray: [GKScore] = [easyScoreReporter]

    GKScore.reportScores(scoreArray, withCompletionHandler: {(NSError) -> Void in
        if NSError != nil {
            print(NSError!.localizedDescription)
        } else {
            print("completed Easy")
        }

    })
}

Upvotes: 4

Oliver Shi
Oliver Shi

Reputation: 148

I figured out how to do it.

if GKLocalPlayer.localPlayer().authenticated == true{
        var highScore = userDefaults.integerForKey("highScore")
        var scoreReporter = GKScore(leaderboardIdentifier: "myLeaderboardID")
        scoreReporter.value = Int64(highScore)
        var scoreArray: [GKScore] = [scoreReporter]
        //println("report score \(scoreReporter)")
        GKScore.reportScores(scoreArray, {(error : NSError!) -> Void in
            if error != nil {
                NSLog(error.localizedDescription)
            }
        })


    }

Upvotes: 4

Related Questions