Game Centre - Submitting to a Non-Default Leaderboard

55 views Asked by At

So I have an App that has a Game A and Game B.

I have Game Centre implemented correctly for Game A (I used the AppCoda tutorial like I have for every game so far).

Now I'm having troubles getting it to submit the score if Game B is played. I need to the score to be submitted to the second leaderboard created in iTunes Connect.

This is my part of my ViewController that authenticates the User and uses the identifier for the leaderboard etc.

ViewController.h:

-(void)authenticateLocalPlayer{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
    if (viewController != nil) {
        [self presentViewController:viewController animated:YES completion:nil];
    }
    else{
        if ([GKLocalPlayer localPlayer].authenticated) {
            _gameCenterEnabled = YES;                                   //added bool indentier to .h

            // Get the default leaderboard identifier.
            [[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) {

                if (error != nil) {
                    NSLog(@"%@", [error localizedDescription]);
                }
                else{
                    _leaderboardIdentifier = leaderboardIdentifier;     //added pointer to NSString to .h
                }
            }];
        }

        else{
            _gameCenterEnabled = NO;
        }
    }
};

}

Seems my Game B View Controller is scorings/submitting just like Game A, I figured I could just change the above code to this:(to allow for the second identifier):

-(void)authenticateLocalPlayer{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];

localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
    if (viewController != nil) {
        [self presentViewController:viewController animated:YES completion:nil];
    }
    else{
        if ([GKLocalPlayer localPlayer].authenticated) {
            _gameCenterEnabled = YES;                                   //added bool indentier to .h
            _gameCenterEnabled2 = YES;

            // Get the default leaderboard identifier.
            [[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) {

                if (error != nil) {
                    NSLog(@"%@", [error localizedDescription]);
                }
                else{
                    _leaderboardIdentifier = leaderboardIdentifier;     //added pointer to NSString to .h
                }
            }];

            // Get the second leaderboard identifier.
            [[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier2, NSError *error) {

                if (error != nil) {
                    NSLog(@"%@", [error localizedDescription]);
                }
                else{
                    _leaderboardIdentifier2 = leaderboardIdentifier2;     //added pointer to NSString to .h
                }
            }];


        }

        else{
            _gameCenterEnabled = NO;
            _gameCenterEnabled2 = NO;
        }
    }
};

}

But for some reason, it won't send the score to the second leaderboard and I can't find any resources/tutorials on how to submit a score to a "non-default" leaderboard...

1

There are 1 answers

0
Reanimation On BEST ANSWER

Ok, so I re-read the Apple Doc and found the solution.

Obviously there can only be ONE default leaderboard (which is authenticated and set in the code in my question)... This doesn't need changing like I originally thought. (I forgot it was used to set the default board).

So what I needed to do was set the Leaderboard identifier to the second leaderboard's identifier (this will be whatever ID you used in iTunes Connect when making the second leaderboard).

I did it in the Game B View Controller when reporting a score like this:

-(void)reportScore{

//set identifier manually as it's not the default leaderboard
GKScore *score = [[GKScore alloc] initWithLeaderboardIdentifier:@"The_GameB_Leaderboard"];
//GKScore *score = [[GKScore alloc] initWithLeaderboardIdentifier:_leaderboardIdentifier2];

score.value = ScoreNumber_B; //Game B HighScore

[GKScore reportScores:@[score] withCompletionHandler:^(NSError *error) {
    if (error != nil) {
        NSLog(@"%@", [error localizedDescription]);
    }
}];
NSLog(@"Reported to Game Center...");

}

There's no need to change the -(void)authenticateLocalPlayer method unless you need to add the GameB bool like I did, in which case you can add it below the GameA Bool:

if ([GKLocalPlayer localPlayer].authenticated) {
        _gameCenterEnabled = YES;      //added bool indentier to .h
        _gameCenterEnabled2 = YES;     //GameB bool
    .
    .
    .
}

I hope this helps.