Report Achievement

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 the following code:

 // Report Achievement
 func​ ​reportAchievements​(achievements: [​GKAchievement​],
  errorHandler: ((​Error​?)->​Void​)? = ​nil​) {
 guard​ ​GKLocalPlayer​.local.isAuthenticated ​else​ { ​return​ }
 
 GKAchievement​.​report​(achievements, withCompletionHandler: errorHandler)
 }

This is the method you’ll call from the game over scene to report the player’s achievements; you still need to call this method.

Switch back to the GameOverScene.swift file, and below the code that report’s the score, add the following code:

 // Get and report achievements
 var​ achievements: [​GKAchievement​] = []
 achievements.​append​(​AchievementsHelper​.​firstWinAchievement​(didWin: isWinner))
 GameKitHelper​.shared.​reportAchievements​(achievements: achievements)

This code creates an array of achievements and passes it into the method you created to report achievements; it also causes Xcode to complain about a missing GKAchievement type:

images/AddingLeaderboardsAndAchievements/xcode-error.png

To resolve this error, you need to import the GameKit framework. Scroll up to the top of the file and add the import statement:

 import​ ​GameKit

Build and run the project, and when you win the game this time, you’ll see the Achievement banner at the top of the scene:

images/AddingLeaderboardsAndAchievements/build-gc-achievement-win.png

Now, tap the Achievements button and you’ll see that you earned the “First Win” achievement:

images/AddingLeaderboardsAndAchievements/build-gc-achievements-success.png

Congratulations, you’re a winner—and you’re well on your way to learning the inner workings of Game Center and the GameKit framework.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset