4

ive updated my app for IOS 7 and game center has a few things deprecated such as loading and dismissing the leaderboard and achievements how can i fix them it says GKLeaderboardViewController is deprecated

- (IBAction)LeaderBoardsButton:(id)sender {
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != NULL)
{
    leaderboardController.leaderboardDelegate = self;
    [self presentViewController:leaderboardController animated:YES completion:NULL];
}
{
    AudioServicesPlaySystemSound(SoundID);
}
}

- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
[self dismissViewControllerAnimated:YES completion:NULL];
{
    AudioServicesPlaySystemSound(SoundID2);
}
}

- (IBAction)AchievementsButton:(id)sender {
GKAchievementViewController *achievements = [[GKAchievementViewController
                                              alloc] init];
if (achievements != nil)
{
    achievements.achievementDelegate = self;
    [self presentViewController:achievements animated:YES completion:NULL];
}
{
    AudioServicesPlaySystemSound(SoundID);
}
}

- (void)achievementViewControllerDidFinish:(GKAchievementViewController
                                        *)viewController
{
[self dismissViewControllerAnimated:YES completion:NULL];
{
    AudioServicesPlaySystemSound(SoundID2);
}
}

i am reporting the score like this

- (IBAction)ShareScore:(id)sender {
[self.gameCenterManager reportScore: counter forCategory: self.currentLeaderBoard];

GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != NULL)
{
    leaderboardController.category = self.currentLeaderBoard;
    leaderboardController.timeScope = GKLeaderboardTimeScopeToday;
    leaderboardController.leaderboardDelegate = self;
    [self presentViewController:leaderboardController animated:YES completion:NULL];
}
{
    AudioServicesPlaySystemSound(SoundID);
}
}
Greg
  • 33,450
  • 15
  • 93
  • 100
Azabella
  • 837
  • 2
  • 10
  • 18

1 Answers1

22

iOS 7 combines the leaderboards, achievements, etc. controllers together into the GKGameCenterViewController class. You use the viewState parameter to control which view you want displayed.

You'll want to do something like this to present/dismiss the leaderboards:

- (void) presentLeaderboards {
    GKGameCenterViewController* gameCenterController = [[GKGameCenterViewController alloc] init];
    gameCenterController.viewState = GKGameCenterViewControllerStateLeaderboards;
    gameCenterController.gameCenterDelegate = self;
    [self presentViewController:gameCenterController animated:YES completion:nil];
}

- (void) gameCenterViewControllerDidFinish:(GKGameCenterViewController*) gameCenterViewController {
    [self dismissViewControllerAnimated:YES completion:nil];
}

Similarly, for presenting achievements, you can do this:

- (void) presentAchievements {
    GKGameCenterViewController* gameCenterController = [[GKGameCenterViewController alloc] init];
    gameCenterController.viewState = GKGameCenterViewControllerStateAchievements;
    gameCenterController.gameCenterDelegate = self;
    [self presentViewController:gameCenterController animated:YES completion:nil];
}

Reporting score would look something like this:

- (void) reportHighScore:(NSInteger) highScore forLeaderboardId:(NSString*) leaderboardId {
    if ([GKLocalPlayer localPlayer].isAuthenticated) {
        GKScore* score = [[GKScore alloc] initWithLeaderboardIdentifier:leaderboardId];
        score.value = highScore;
        [GKScore reportScores:@[score] withCompletionHandler:^(NSError *error) {
            if (error) {
                NSLog(@"error: %@", error);
            }
        }];
    }
}
Greg
  • 33,450
  • 15
  • 93
  • 100
  • hi greg that worked perfect i edited the question as i missed out the reporting score section how would i tackle that? – Azabella Oct 02 '13 at 17:47
  • how would i trigger the void statement as i want it triggered when the game ends and when they submit there score – Azabella Oct 02 '13 at 17:53
  • 1
    Do you mean how do you call the reportHighScore:forLeaderboardId: method? That would be analogous to this line that you wrote above: [self.gameCenterManager reportScore: counter forCategory: self.currentLeaderBoard];. Instead you would do something like [self reportHighScore:counter forLeaderboardId:self.currentLeaderBoard];. – Greg Oct 02 '13 at 17:56
  • you sir are a beautiful man i applaud you and send you a virtual Hi5 – Azabella Oct 02 '13 at 18:26
  • @Greg what is Highscore – Jacobanks Feb 21 '14 at 06:51
  • whoops like in score.value = Highscore; – Jacobanks Feb 21 '14 at 06:51
  • It's the first parameter of the reportHighScore:forLeaderboardId: method in my example. The documentation for GKScore's value property is here: https://developer.apple.com/library/ios/documentation/GameKit/Reference/GKScore_Ref/Reference/Reference.html#//apple_ref/occ/instp/GKScore/value – Greg Feb 21 '14 at 19:14