Topics

#61: Game Center Achievements 🏆

Topics

Continuing where we left off with Game Center in Bite #57, let's add support for achievements to our game. We start by adding a couple of achievements in iTunes Connect. When viewing our app, we'll click Game Center, then Add Achievement.

Most of this process is self-explanatory, but one thing to note is that each achievement can have a point value of no more than 100. Additionally, the point values of all the achievements in our game added together can't be more than 1000.

Now all we need to do is tell Game Center when a player completes (or makes progress towards) an achievement. Every time our user scores a point, we'll update their progress.

if score <= 5 {
  let achievement = GKAchievement(identifier: "achievement.womprats.five")

  achievement.percentComplete = Double(score / 5)
  achievement.showsCompletionBanner = true  // use Game Center's UI

  GKAchievement.reportAchievements([achievement], withCompletionHandler: nil)
}

We can also load the user's current achievement progress anytime like this:

GKAchievement.loadAchievementsWithCompletionHandler() { achievements, error in
  guard let achievements = achievements else { return }

  print(achievements)
}

Remember that the local player will need to be authenticated (as shown in Bite #57) before you can load or report any achievements.