问题
Has anyone come across this error when trying to integrate Game Center into an iOS 7 App?
A GKScore must specify a leaderboard.
Here is the code where it fails:
if(points > 0)
{
//Fails on the next line
[self.gameCenterManager reportScore:points forCategory:self.currentLeaderBoard];
}
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != NULL) {
leaderboardController.category = self.currentLeaderBoard;
leaderboardController.timeScope = GKLeaderboardTimeScopeWeek;
leaderboardController.leaderboardDelegate = self;
[self presentViewController:leaderboardController animated:YES completion:nil];
}
UPDATE I have tried another method and still get the same error.
Other Method:
GKScore *scoreReporter = [[GKScore alloc] initWithCategory:self.currentLeaderBoard];
scoreReporter.value = points;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
if (error != nil)
{
[self showAlert:@"Game Center Error" theMessage:@"There was a problem uploading your score to Game Center, if this problem persists, please contact JApp Design." alertTag:0];
}
}];
Any ideas?
回答1:
I had a same issue with you but I solved my issue..
Here is my original code.
GKScore *scoreReporter = [[GKScore alloc] initWithLeaderboardIdentifier: identifier];
scoreReporter.value = score;
scoreReporter.context = 0;
scoreReporter.shouldSetDefaultLeaderboard = YES;
NSArray *scores = @[scoreReporter];
[GKScore reportScores:scores withCompletionHandler:^(NSError *error) {
//Do something interesting here.
[self callDelegateOnMainThread: @selector(scoreReported:) withArg: NULL error: error];
}];
From here, I removed the line. scoreReporter.shouldSetDefaultLeaderboard = YES
So it worked fine.
回答2:
This method of reporting scores appears to be deprecated with the introduction of iOS 7.0
Here is the code that will fix the problem:
GKScore *reportScore = [[GKScore alloc] initWithLeaderboardIdentifier:@"leaderBoardI"];
reportScore.value = totalPoints;
NSArray *scores = @[reportScore, nil];
[GKScore reportScores:scores withCompletionHandler:nil];
来源:https://stackoverflow.com/questions/22273320/a-gkscore-must-specify-a-leaderboard