Reporting Scores to Leaderboards
Problem
You have created at least one leaderboard in iTunes Connect and now you want to store players’ scores to that leaderboard.
Solution
Use the reportScoreWithCompletionHandler:
instance
method of the GKScore
class as
demonstrated in the Discussion section.
Discussion
Assuming that you have already created a leaderboard (see Creating Leaderboards in iTunes Connect), you must follow these steps to report scores to it:
Authenticate the local player (see Authenticating the Local Player in Game Center).
Create an instance of the
GKScore
class and set the category of that score to the Leaderboard ID that you chose when you were creating this leaderboard.Set the
value
property of the score object.Use the
reportScoreWithCompletionHandler:
instance method of theGKScore
class to report the error. This method accepts one parameter, which must be a block that returnsvoid
and accepts a parameter of typeNSError
. You can use this error to determine whether an error occurred during the process of reporting the score:
- (BOOL) reportScore:(NSUInteger)paramScore toLeaderboard:(NSString *)paramLeaderboard{ __block BOOL result = NO; GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; if ([localPlayer isAuthenticated] == NO){ NSLog(@"You must authenticate the local player first."); return NO; } if ([paramLeaderboard length] == 0){ NSLog(@"Leaderboard identifier is empty."); return NO; } GKScore *score = [[[GKScore alloc] initWithCategory:paramLeaderboard] autorelease]; ...
Get Writing Game Center Apps in iOS now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.