Home How to integrate GameKit
Post
Cancel

How to integrate GameKit

What is it?

GameKit is a group of APIs from apple to simplify some games basic features like leaderboards, achievements, and online matches.

In this tutorial, I’m going to show you how to integrate GameKit with your game, So you can add achievements and make a leaderboard to show the top players.

Let’s start!

First, you need to open your project then after that navigate to your app target then select capabilities then turn on Game Center.

After that, we will go to the screen that we will show the leaderboard and achievement screens from it.

I have already designed the screen and added buttons to show the leaderboards and achievements screens.

Before doing anything in the ViewController that you will add the GameKit to, you need to import GameKit.

Authenticate the user

To be able to submit a new score or unlock an achievement you need to authenticate the user, to do that we need to get the local player then set the authenticateHandler property.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
override func viewDidLoad() {
  super.viewDidLoad()
  authenticateUser()
}

func authenticateUser() {
  let player = GKLocalPlayer.local
  player.authenticateHandler = { vc, error in
    guard error == nil else {
      print(error?.localizedDescription ?? "")
      return
    }
    self.present(vc, animated: true, completion: nil)
  }
}

The authenticateHandler will give us a view controller we need to present it.

To open the achievements screen we will make an object from the GKGameCenterViewController class, and we will set the delegate as the current view controller and the viewState as .achievements.

1
2
3
4
let vc = GKGameCenterViewController()
vc.gameCenterDelegate = self
vc.viewState = .achievements
present(vc, animated: true, completion: nil)

At this point you will get an error saying:

1
Cannot assign value of type ‘GameKitHelper’ to type ‘GKGameCenterControllerDelegate?’

To fix this error you need to implement the GKGameCenterControllerDelegate protocol.

1
2
3
4
5
extension ViewController: GKGameCenterControllerDelegate {
  func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) {
    gameCenterViewController.dismiss(animated: true, completion: nil)
  }
}

And the same thing for the leaderboards we need to make an object from the GKGameCenterViewController class, but the difference that we will set extra property and it’s the leaderboardIdentifier, we will get this property later from iTunes connect.

1
2
3
4
5
let vc = GKGameCenterViewController()
vc.gameCenterDelegate = self
vc.viewState = .leaderboards
vc.leaderboardIdentifier = "leaderboardID"
present(vc, animated: true, completion: nil)

The final look of the view controller after adding everything:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import UIKit
import GameKit
class ViewController: UIViewController {
override func viewDidLoad() {
  super.viewDidLoad()
  authenticateUser()
}
func authenticateUser() {
  let player = GKLocalPlayer.local
  player.authenticateHandler = { vc, error in
    guard error == nil else {
      print(error?.localizedDescription ?? "")
      return
    }
    self.present(vc, animated: true, completion: nil)
  }
}
@IBAction func leaderboard(_ sender: Any) {
  let vc = GKGameCenterViewController()
  vc.gameCenterDelegate = self
  vc.viewState = .leaderboards
  vc.leaderboardIdentifier = leaderboardID
  present(vc, animated: true, completion: nil)
}
@IBAction func achievements(_ sender: Any) {
  let vc = GKGameCenterViewController()
  vc.gameCenterDelegate = self
  vc.viewState = .achievements
  present(vc, animated: true, completion: nil)
}
}
extension ViewController: GKGameCenterControllerDelegate {
func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) {
  gameCenterViewController.dismiss(animated: true, completion: nil)
}
}

Now if you run the app nothing will happen because still, we need to do more things to get it works.

Now you need to go to the iTunes Connect, Then select your app, then select features, then game center, now from here you can add an achievement or a leaderboard it’s a very simple process.

For adding a new leader board click on the add icon, then select Single Leaderboard, now enter the name, id, and the score format type, for the Score Submission Type it’s to decide which score should the leaderboard keep, the latest one or the highest one. One last thing, you need to add a language, go to Leaderboard Localization and click on add language, a new dialog will open select the language and enter the name, score format, and score suffix.

Like this, we have added our first leaderboard.

Now for the achievements, it’s almost the same thing click on add achievement icon, enter the name, id, and points and don’t forget to add a language from the Achievement Localization section.

Now run the app, click on achievements or leaderboards you will see that a new screen will open with the leaderboards and achievements.

To submit a new point to the leader board, we will make an object from the GKScore class, this class takes the leaderboard id as a parameter. after making the object we need to but the value and then report this score by calling report method.

1
2
3
4
5
6
7
8
9
10
11
func report(score: Int64) {
  let reportedScore = GKScore(leaderboardIdentifier: leaderboardID)
  reportedScore.value = score
  GKScore.report([reportedScore]) { (error) in
    guard error == nil else {
    print(error?.localizedDescription ?? "")
    return
    }
  print("The score submitted to the game center"). 
  }
}

To report an achievement, we will make an object from the GKAchievement class, this class takes an identifier as a parameter. we can get this identifier from the iTunes Connect, you can select any achievement you want to report. after that, you need to set the percentComplete if you want the user to get the achievement directly just set it as 100. then we will report this achievemnet by calling report method.

1
2
3
4
5
6
7
8
func report(achievement: Achievement, percentComplete: Double) {
  let achievement = GKAchievement(identifier: achievement.identifier)
  achievement.percentComplete = percentComplete
  achievement.showsCompletionBanner = true
  GKAchievement.report([achievement]) { (error) in
    print(error?.localizedDescription ?? "")
  }
}

That’s it

So that’s it, now your users can enjoy these beautiful features!!

-❤️~.
If you have any questions you can send me a message on Twitter or facebook. Also you can check my Github page or my Apps.

This post is licensed under CC BY 4.0 by the author.