GKLeaderboard Class Reference
| Inherits from | |
| Conforms to | |
| Framework | /System/Library/Frameworks/GameKit.framework |
| Availability | Available in iOS 4.1 and later. |
| Companion guide | |
| Declared in | GKLeaderboard.h |
Overview
A GKLeaderboard object is used to read data from a leaderboard stored on Game Center. Your game uses GKLeaderboard objects when it wants to retrieve localized information about a specific leaderboard or to retrieve scores from a leaderboard. Typically, you do this when you want the data needed to implement your own custom leaderboard user interface.
During the development process, you create the leaderboards for your game on iTunes Connect.
To retrieve information about the available leaderboards, use the loadLeaderboardsWithCompletionHandler: class method.
To request score data from Game Center, your game allocates and initializes a GKLeaderboard object, configures its search properties, and then calls the object’s loadScoresWithCompletionHandler: method. Game Kit retrieves the score data from Game Center and calls your completion handler.
The search properties are used to filter the score data returned to your game:
The
categoryproperty allows you to choose which leaderboard on iTunes Connect is searched.The
playerScopeproperty allows you to restrict the search to a local player’s friends. Optionally, you can also explicitly initialize a leaderboard object to search for scores for a specific group of players.The
timeScopeproperty allows you to filter based on when the score was earned.The
rangeproperty allows you to pick scores within a specific range. For example, the range [1,10] returns the best ten scores.
The algorithm used by the GKLeaderboard object is as follows:
Start with the set of all possible scores.
Discard any scores that do not match the
playerScope,timeScopeandcategoryproperties.For each player, keep the best score that player has earned and discard the rest.
Sort the scores from best to worst.
Use the range property to determine which scores are returned.
Tasks
Determining the Available Leaderboards to Display
-
+ loadLeaderboardsWithCompletionHandler: -
+ loadCategoriesWithCompletionHandler:Deprecated in iOS 6.0
Initialization
Customizing the Leaderboard Request
-
playerScopeproperty -
rangeproperty -
timeScopeproperty -
categoryproperty
Retrieving High Scores
-
– loadScoresWithCompletionHandler: -
loadingproperty -
titleproperty -
scoresproperty -
localPlayerScoreproperty -
maxRangeproperty
Changing a Local Player’s Default Leaderboard
Retrieving Group Information
-
groupIdentifierproperty
Properties
category
The named leaderboard to retrieve information from.
Discussion
If non-nil, Game Center only returns scores from the matching leaderboard. If nil, all scores previously reported by the game are searched. Default is nil.
Availability
- Available in iOS 4.1 and later.
Declared In
GKLeaderboard.hgroupIdentifier
The identifier for the group the leaderboard is part of.
Discussion
If your game was configured to be part of a group in iTunes Connect, this property holds the identifier you assigned to the group.
Availability
- Available in iOS 6.0 and later.
Declared In
GKLeaderboard.hloading
A Boolean value that indicates whether the leaderboard object is retrieving scores. (read-only)
Discussion
The value of the loading property is YES if the leaderboard object has any pending requests for scores.
Availability
- Available in iOS 4.1 and later.
Declared In
GKLeaderboard.hlocalPlayerScore
The score earned by the local player. (read-only)
Discussion
This property is invalid until a call to loadScoresWithCompletionHandler: is completed. Afterward, it contains a score object representing the local player’s score on the leaderboard given the filters you applied to the query.
Availability
- Available in iOS 4.1 and later.
Declared In
GKLeaderboard.hmaxRange
The size of the leaderboard. (read-only)
Discussion
This property is invalid until a call to loadScoresWithCompletionHandler: is completed. Afterward, it contains the total number of entries available to return to your game given the filters you applied to the query.
Availability
- Available in iOS 4.1 and later.
Declared In
GKLeaderboard.hplayerScope
A filter used to restrict the search to a subset of the players on Game Center.
Discussion
The playerScope property is ignored if the leaderboard request was initialized using the initWithPlayerIDs: method. Otherwise, the playerScope property determines which players are included in the request for high scores. The default is GKLeaderboardPlayerScopeGlobal. See Leaderboard Player Scope for more information.
Availability
- Available in iOS 4.1 and later.
Declared In
GKLeaderboard.hrange
The numerical score rankings to return from the search.
Discussion
The range property is ignored if the leaderboard request was initialized using the initWithPlayerIDs: method. Otherwise, the range property is used to filter which scores are returned to your game. For example, if you specified a range of [1,10], after the search is complete, your game receives the best ten scores. The default range is [1,25].
The minimum index is 1. The maximum length is 100.
Availability
- Available in iOS 4.1 and later.
Declared In
GKLeaderboard.hscores
The list of scores returned by the search. (read-only)
Discussion
This property is invalid until a call to loadScoresWithCompletionHandler: is complete. Afterward, it contains the same score objects that were returned to the completion handler.
Availability
- Available in iOS 4.1 and later.
Declared In
GKLeaderboard.htimeScope
A filter used to restrict the search to scores that were posted within a specific period of time.
Discussion
This property determines how far back in time to look for scores. The default value is GKLeaderboardTimeScopeAllTime. See Leaderboard Time Scope for more information.
Availability
- Available in iOS 4.1 and later.
Declared In
GKLeaderboard.htitle
The localized title for the leaderboard. (read-only)
Discussion
If you initialized a new leaderboard object, this property is invalid until a call to loadScoresWithCompletionHandler: is complete. Afterward, it contains the localized title for the leaderboard identified by the category property.
Availability
- Available in iOS 4.1 and later.
Declared In
GKLeaderboard.hClass Methods
loadLeaderboardsWithCompletionHandler:
Loads the list of leaderboards from Game Center
Parameters
- completionHandler
A block that is called when the categories have been retrieved from the server.
The block receives the following parameters:
- leaderboards
An array of
GKLeaderboardobjects that provides the leaderboards for your game. If an error occurred, this value may be non-nil. In this case, the array holds whatever data Game Kit was able to download before the error occurred.- error
If an error occurred, this error object describes the error. If the operation completed successfully, the value is
nil.
Discussion
Use this class method to retrieve the list of leaderboards you configured on iTunes Connect. Use the properties of each leaderboard object, especially the category and title properties, to learn more about the leaderboard.
When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. Keep in mind that the completion handler may be called on a thread other than the one originally used to invoke the method. This means that the code in your block needs to be thread-safe.
Availability
- Available in iOS 6.0 and later.
Declared In
GKLeaderboard.hsetDefaultLeaderboard:withCompletionHandler:
Sets the default leaderboard for the local player.
Parameters
- categoryID
The named leaderboard that should be the new default leaderboard for the local player.
- completionHandler
A block to be called after the scores are retrieved from the server.
The block receives the following parameter:
- error
If an error occurred, this error object describes the error. If the operation was completed successfully, the value is
nil.
Discussion
The default leaderboard is used whenever your game uses a GKScore object to report a score to Game Center without explicitly setting the score object’s category property. The default leaderboard is normally set in iTunes Connect. However, your game can use this class method to override the default leaderboard that appears for the local player. This information is stored for each player on Game Center.
When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. Keep in mind that the completion handler may be called on a thread other than the one originally used to invoke the method. This means that the code in your block needs to be thread-safe.
If an error occurs and was a network error, your game should periodically resend the request until it completes.
Availability
- Available in iOS 5.0 and later.
Declared In
GKLeaderboard.hInstance Methods
init
Initializes a default leaderboard request.
Return Value
An initialized leaderboard request.
Discussion
A leaderboard object initialized with this method uses the playerScope, timeScope, and range properties to search Game Center for scores.
Availability
- Available in iOS 4.1 and later.
Declared In
GKLeaderboard.hinitWithPlayerIDs:
Initializes a leaderboard request to retrieve the scores of a specific group of players.
Parameters
- playerIDs
An array of
NSStringobjects that holds the player identifier strings of the players to retrieve.
Return Value
An initialized leaderboard request.
Discussion
A leaderboard object initialized with this method ignores the playerScope and range properties. Instead, it retrieves scores for the specific list of players whose identifiers are included in the playerIDs parameter.
Availability
- Available in iOS 4.1 and later.
Declared In
GKLeaderboard.hloadScoresWithCompletionHandler:
Retrieves a set of scores from Game Center.
Parameters
- completionHandler
A block to be called after the scores are retrieved from the server.
The block receives the following parameters:
- scores
An array of score objects that hold the requested scores. If an error occurred, this value may be non-
nil. In this case, the array holds whatever score data could be retrieved from Game Center before the error occurred.- error
If an error occurred, this error object describes the error. If the operation was completed successfully, the value is
nil.
Discussion
When this method is called, it creates a new background task to handle the request. The method then returns control to your game. Later, when the task is complete, Game Kit calls your completion handler. Keep in mind that the completion handler may be called on a thread other than the one originally used to invoke the method. This means that the code in your block needs to be thread-safe.
You can call this method multiple times; each call represents a different query against the scores stored on Game Center. If you post multiple load operations using the same leaderboard object, any properties that are updated by loading scores reflect the last query that completed. The order that achievement queries are processed is arbitrary.
Availability
- Available in iOS 4.1 and later.
Declared In
GKLeaderboard.hConstants
Leaderboard Time Scope
The period of time to which a player’s best score is restricted.
enum {
GKLeaderboardTimeScopeToday = 0,
GKLeaderboardTimeScopeWeek,
GKLeaderboardTimeScopeAllTime
};
typedef NSInteger GKLeaderboardTimeScope;
Constants
GKLeaderboardTimeScopeTodayEach player is restricted to scores recorded in the past 24 hours.
Available in iOS 4.1 and later.
Declared in
GKLeaderboard.h.GKLeaderboardTimeScopeWeekEach player is restricted to scores recorded in the past week.
Available in iOS 4.1 and later.
Declared in
GKLeaderboard.h.GKLeaderboardTimeScopeAllTimeEach player’s best score is returned.
Available in iOS 4.1 and later.
Declared in
GKLeaderboard.h.
Availability
- Available in iOS 4.1 and later.
Declared In
GKLeaderboard.hLeaderboard Player Scope
The scope of players to be searched for scores.
enum {
GKLeaderboardPlayerScopeGlobal = 0,
GKLeaderboardPlayerScopeFriendsOnly
};
typedef NSInteger GKLeaderboardPlayerScope;
Constants
GKLeaderboardPlayerScopeGlobalAll players on Game Center should be considered when generating the list of scores.
Available in iOS 4.1 and later.
Declared in
GKLeaderboard.h.GKLeaderboardPlayerScopeFriendsOnlyOnly friends of the local player should be considered when generating the list of scores.
Available in iOS 4.1 and later.
Declared in
GKLeaderboard.h.
Availability
- Available in iOS 4.1 and later.
Declared In
GKLeaderboard.h© 2012 Apple Inc. All Rights Reserved. (Last updated: 2012-09-19)