How to get local player score from Game Center

game-center, gamekit, ios7, iphone, objective-c

Solution

Your code appears to not have any bugs that I can see. I would recommend displaying the standard leaderboard interface to see if your code that reports the scores is actually working correctly. If so, you should see the scores in the leaderboard. The code below works in my game, and I know the score reporting is working properly because it shows in the default game center UI.

GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
leaderboardRequest.identifier = kLeaderboardCoinsEarnedID;
[leaderboardRequest loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
    if (error) {
        NSLog(@"%@", error);
    } else if (scores) {
    GKScore *localPlayerScore = leaderboardRequest.localPlayerScore;
    CCLOG(@"Local player's score: %lld", localPlayerScore.value);
    }
}];

If you aren't sure how, the code below should work to show the default leaderboard (iOS7):

 GKGameCenterViewController *gameCenterVC = [[GKGameCenterViewController alloc] init];
 gameCenterVC.viewState = GKGameCenterViewControllerStateLeaderboards;
 gameCenterVC.gameCenterDelegate = self;
 [self presentViewController:gameCenterVC animated:YES completion:^{
      // Code
 }];

Problem

How to get score of local player from Leaderboard Game Center? I tried this code, but it returns nothing. Anybody know how to solve it, or is there better way how to get score? ``` - (NSString*) getScore: (NSString*) leaderboardID { __block NSString *score; GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init]; if (leaderboardRequest != nil) { leaderboardRequest.identifier = leaderboardID; [leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) { if (error != nil) { NSLog(@"%@", [error localizedDescription]); } if (scores != nil) { int64_t scoreInt = leaderboardRequest.localPlayerScore.value; score = [NSString stringWithFormat:@"%lld", scoreInt]; } }]; } return score; } ``` I think, that method have to wait for completion of [leaderboardRequest loadScoresWithCompletionHandler: ... Is it possible?

Original source