Report Achievement
Apple Game Frameworks and Technologies — by Tammy Coron (158 / 193)
👈 Report Score to Leaderboard | TOC | Next Steps 👉
Achievements are slightly different in how they are reported, as they require a bit more code than leaderboard score reporting. First, switch back to the GameKitHelper.swift file.
You’ll start by creating a new class. You could put this new class in a file of its own, but because there’s only one achievement in Hog Dice, you’ll add it to the GameKitHelper.swift file instead.
Above the delegate extension mark, add the following code:
// MARK: - ACHIEVEMENTS HELPER CLASS
class AchievementsHelper {
static let achievementIdFirstWin = "net.justwritecode.hogdice.first.win"
class func firstWinAchievement(didWin: Bool) -> GKAchievement {
let achievement = GKAchievement(
identifier: AchievementsHelper.achievementIdFirstWin)
if didWin {
achievement.percentComplete = 100
achievement.showsCompletionBanner = true
}
return achievement
}
}
Here, you’re creating a static property to hold the Achievement ID (remember to update that value to match your Achievement ID). You’re then creating a GKAchievement object and setting its properties depending on whether the player won the game.
Now, in the GameKitHelper class, just below the method that reports the player’s score, add…