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
GKScoreclass and set the category of that score to the Leaderboard ID that you chose when you were creating this leaderboard.Set the
valueproperty of the score object.Use the
reportScoreWithCompletionHandler:instance method of theGKScoreclass to report the error. This method accepts one parameter, which must be a block that returnsvoidand 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]; ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access