One of the functionalities in Game Center is the ability to manage leaderboards in your iOS apps. For instance, you can write a racing game for iOS and have players compete to achieve the best score. You can then report these scores to a leaderboard and allow the players to see the leaderboard. This gives your players a reason to come back to your app (in order to compete with their friends).
To use leaderboards in your app, you must first create them for your app in iTunes Connect. Here is how you can do that:
- Go to the Apple Developer Portal and select iTunes Connect from the righthand side of the screen.
- In iTunes Connect, select Manage Your Applications.
- In Manage Your Applications, select the app you want to add a leaderboard to. To add a leaderboard to an app, you must have already enabled Game Center for it (Figure 1-4).
- Once in the app in iTunes Connect, select the Manage Game Center button on the righthand side of the screen.
- Under Leaderboard box, select the Set Up button.
- Select the Add Leaderboard button on the top lefthand corner.
There are two types of leaderboards in Game Center:
Follow these steps to create a single leaderboard that can contain scores from 1 to 1,000, with 1,000 being the highest score:
- Select the Choose button in the Single Leaderboard category.
- In the Leaderboard Reference Name box, enter a name that you would like to use to refer to this leaderboard. This will not be the name you will be using to refer to this leaderboard in your code. This is simply a name which you choose and can see later in iTunes Connect. Pick a descriptive name such as “My Game’s Level 1 Leaderboard.”
- In the Leaderboard ID box, enter the ID that you will use later in your code to refer to this leaderboard. For instance, I could pick MGL1LB (referring to “My Game’s Level 1 Leaderboard,” which I picked as the reference name). You don’t have to do as I did; simply pick a reference ID, but remember that this will be the ID of this leaderboard you use in your app.
- In the Score Format Type dropdown, pick Integer, since we want just to report scores ranging from 1 to 1,000.
- For Sort Order, pick Descending because we want the highest score (1,000) to be displayed on top and score 1 to be at the bottom. If you want the lowest score (1) to be displayed on top of the leaderboard, pick Ascending for this option.
- In the Score Range box, set the left box’s value to 1 and the right box’s value to 1,000. That will define the score range that your application will report to this specific leaderboard. Any other score which is not in this range will automatically get deleted by Game Center.
Note: Each leaderboard in Game Center has its own localization, managed by iTunes Connect. For instance, if your app supports English and Italian localizations, you will want your leaderboard data to be presented to the players in each one of these countries in their own language. For instance, displaying “120 points” to an Italian player is not very nice. The “points” part is English and should not be displayed to Italian players. iTunes Connect allows you to specify these suffixes and prefixes for your scores depending on what localization the player has enabled on his device. The good thing is that once you enter these values in iTunes Connect, Game Center APIs on the device will automatically fetch the correctly localized values from Game Center servers, depending on the localization of the current device. You won’t have to detect the localization at run time.
- Select the Add Language button (Figure 1-9).
Figure 1-9. Adding English localization to a leaderboard in iTunes Connect
- Pick English from the Language box.
- In the Name box, pick a name (that gets displayed to the player) for this leaderboard. For instance, I picked Level 1.
- In the Score Format, pick 100,000,122.
- In the Score Format Suffix (Singular), write “point” without the quotation marks. This gets attached to the end of scores that are 1. For instance, if the player scores 1 in Level 1 of your game, you want to display “1 point” in the leaderboard, whereas for a player who has scored 1,000 points, you want to display “1,000 points“.
- In the Score Format Suffix Plural box, enter “points” without the quotation marks.
- Press the Save button.
- After adding the localizations, press the Save button at the bottom righthand side of the Add Leaderboard screen in iTunes Connect.
Once you have created a leaderboard for your app, you will be able to access it in your app using Game Kit.
Learn more about this topic from Writing Game Center Apps in iOS.
Learn how to integrate Game Center features directly into your iPhone and iPad apps with the recipes in this short and concise cookbook. Each recipe gives you the solution and the code for adding features such as leaderboards, user authentication, achievements, multiplayer games and many others, using Apple's GameKit framework in the iOS SDK.