Looking for good programming challenges?

Use the search below to find our solutions for selected questions!

Implement a Game Center leaderboard (iOS 7)

Sharing is caring!

This tutorial will teach you how to implement a Game Center leaderboard in an iOS 7 app.

Step 1: Importing the GameKit Framework

Assuming you have an existing project you need to import the GameKit Framework. This can be easily done by selecting the project name and under General->Linked Frameworks and Libraries click on the “+” button and search for GameKit.framework. Select the framework and click on “Add“.

Step 2: Create an App ID

Next we need to create an App ID for the app in iOS Dev Center. So, log into your iOS Dev Center account and go to “Certificates, Identifiers & Profiles“. Under “iOS Apps” choose “Identifiers” and click on the “+” button to create a new App ID.

Fill out the App ID Description name and Bundle ID. Game Center should be enabled by default under App Services. When finished click “Continue” and then “Submit”

Step 3: Add the new App in iTunes Connect

Log in into your iTunes Connect account and navigate to “Manage Your Applications“.

Click on “Add New App” (Upper left corner) in order to create a new App.

Fill out the App Information form (Default Language, App Name, SKU Number), select the Bundle ID from Step 2 and click “Continue“.

Select your Availability Date and Price Tier and click “Continue“.

Next you need to fill out Information about the Version such as Version Number, Copyright, etc.

You also need to have a Large App Icon that must be at least 72 DPI, in the RGB color space, and 1024 x 1024 pixels. You can use the dummy icon provided for this tutorial. Screenshots for the 3.5-Inch Retina Display and 4-Inch Retina Display iPhone must also be provided. Screenshots for 3.5-inch iPhone and iPod touch Retina display must be 960×640, 960×600, 640×960 or 640×920 pixels, at least 72 DPI, in the RGB color space, and in the JPG or PNG format. Screenshots for 4-inch iPhone 5 and iPod touch (5th generation) Retina display must be 1136×640, 1136×600, 640×1136 or 640×1096 pixels, at least 72 DPI, in the RGB color space, and in the JPG or PNG format. Again you can use my dummy screenshots: 640×920 and 640×1096. After filling out all the necessary information click on “Save

Step 4: Create a Leaderboard

Back in the App overview click “Manage Game Center“. Next click “Enable for Single Game” to enable Game Center for our new App.
Next we want to setup a Leaderboard by clicking on “Add Leaderboard” in the “Leaderboards” section. Choose single Leaderboard and fill out the Leaderboard Reference Name, Leaderboard ID, Score Format Type, Score Submission Type, Sort Order and optionally Score Range.

The Leaderboard Reference Name is just an internal name. Just name it “Your-App-Name Leaderboard”. The Leaderboard ID is a is a unique alphanumeric identifier. You can use the ID “1”. Score Format Type is just the format that you use to express the score within your game. Choose “Integer”. For the Sort Order choose “High to Low” if you want highest scores displayed first and “Low to High” if you want lowest scores displayed first. The Score Range can be left blank.

Next we need to add a language by clicking on “Add Language“.
Choose English for the Language. The Name is the name of the Leaderboard displayed in Game Center. Set it to “Leaderboard”. Again choose “Integer” for the Score Format. Leave the Score Format Suffix (Singular), Score Format Suffix Plural and Image fields blank. Click “Save” and then “Save” again. After saving click “Done

Step 5: Set the Bundle ID in your project

Go to your Xcode project and open the file named [Your Projet name here]-Info.plist and under Bundle identifier insert the Bundle ID you have created in Step 3.

Step 6: Import the GameCenterManager class files

Next download the GKTapper sample project provided by Apple. We will use the following Class Implementations and Interfaces:

  • GameCenterManager.h
  • GameCenterManager.m
  • AppSpecificValues.h

You can download the modified for iOS 7 files here.

Drag the above files into your project and make sure you check “Copy Items into destination group’s folder (if needed)” and then “Finish“.

Annotate your class in which you want to use Game Center (in my case it is called MyScene), that it conforms to the GKGameCenterControllerDelegate and GameCenterManagerDelegate protocol. So just edit MyScene.h:

Step 7: Adding the code: authenticate the local user

In MyScene.m insert the following code:

This will create a new GameCenterManager object and authenticate the local user with Game Center.

Step 8: Adding the code: submit score to leaderboard

The next thing we want to do is to submit a score to the leaderboard.

Just insert the following code anywhere where you want the score to be submitted to the leaderboard.
reportScore takes two parameters: an int64_t representing the score and a
NSString* representing the leaderboard ID from Step 4.

Step 9: Adding the code: display the highscores/leaderboard

For this step you can insert a button that the user touches in order to display the leaderboard. In the action
you can just insert the following code to display the leaderboard:

Also don’t forget the following GKGameCenterControllerDelegate method to dismiss the Game Center view controller when the user is done interacting with it: