Peter Betz
Peter Betz

Reputation: 31

Swift 5 / iOS 14 / How to implement Game Center achievements

I like to add Game Center achievements to my app.

For testing purposes I created one leaderboard list and two achievements in App Store Connect and enabled them in my app.

Then I authenticated the local player in Game Center and called the Dashboard.

What I see is that one leaderboard is available. Fine. However, 0 / 0 achievements. No achievements available. I assume that I should see 0 / 2 achievements but I have no idea, why they do not show up.

I also tried to report an achievement with GKAchievement.report and it seems to report without error.

But when I try to load any achievement, I get an error that no achievement can be found.

Any ideas what I did wrong or forgot to do?

Screenshot of the Dashboard

Upvotes: 2

Views: 308

Answers (1)

Yusuf
Yusuf

Reputation: 981

Give some time for Apple to sync the data.

After the time, one thing you should consider is if an achievement never reported a progress, you must initialize it yourself in the codeside.

let allHardcodedAchievements = GameCenterAchievementType.allCases
var newAchievements = achievements ?? []

// Add hardcoded achievements if they are not in the array
allHardcodedAchievements.forEach { hardcodedAchievement in
  if !newAchievements.contains(where: { $0.identifier == hardcodedAchievement.id }) {
      newAchievements.append(.init(identifier: hardcodedAchievement.id))
    }
  }

self?.achievements = newAchievements

In the loadAchievement blocks, check if your achievement is in the array returned from Apple. If it is not, add your achievement by calling GKAchievement's init(identifier: String) function.

Once you report any progress on it, it will be returned by the Apple's function from then.

Upvotes: 0

Related Questions