спасибо
спасибо

Reputation: 89

How to implement a leaderboard (Game Center) into project

I'm trying to create a leaderboard in Objective-C with no luck. I've been looking around on here and other sites, but I can't seem to find anything that works. I've created the leaderboard on iTunes Connect, but it's the code that I'm having trouble with. I'm getting warnings that some terms are deprecated.

This is the code that I use:

- (IBAction)ShowLeaderboard:(id)sender {

GKGameCenterViewController *leaderboardController = [[GKGameCenterViewController alloc] init];

if (leaderboardController != nil) { leaderboardController.leaderboardCategory = self;

[self presentModalViewController: leaderboardController animated: YES]; }

}

- (IBAction)SubmitScoreToGameCenter:(id)sender {

GKScore *scoreReporter = [[GKScore alloc] initWithCategory:@"LeaderboardName"];

scoreReporter.value = HighScoreNumber;

[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) { if (error != nil){ NSLog(@"Submitting score failed"); }

    else { NSLog(@"Submitting score succeeded"); } }];

}

- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController

{ [self dismissModalViewControllerAnimated:YES];

}

And this in viewDidLoad:

{

[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) { if (error == nil)

{ NSLog(@"Authentication Successful");

}

    else { NSLog(@"Authentication Failed"); } }];

Upvotes: 0

Views: 374

Answers (1)

Michael Mooney
Michael Mooney

Reputation: 347

If you've actually looked this up, you will find loads of tutorials on this so I don't think you have, or you haven't really learned how to use xCode.

Here's how I'd do the code:

    -(void)submitScore //submit the score to game centre
    {
        GKScore *score = [[GKScore alloc] initWithLeaderboardIdentifier:@"LeaderboardName"]; 
        int64_t GameCenterScore = Score;
        score.value = GameCenterScore;

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


-(void)authentication //log player into game centre
{
    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) {
                NSLog(@"authentication succcesful");
                GameCenterAvaliable = YES;
                [[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) {

                    if (error != nil) {
                        NSLog(@"%@", [error localizedDescription]);
                    }
                    else{
                        leaderboardIdentifier = leaderboardIdentifier;
                    }
                }];
            }

            else{
                NSLog(@"authentication unseuccseful");
                GameCenterAvaliable = NO;
            }
        }
    };
}

-(IBAction)ShowGameCenter:(id)sender //show game centre
{
    GKLeaderboardViewController *LeaderboardController = [[GKLeaderboardViewController alloc] init];
    if (LeaderboardController != nil) {
        LeaderboardController.leaderboardDelegate = self;
        [self presentViewController:LeaderboardController animated:YES];
    }
}

//Animate gc out if finished with it

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

That's for sending your score to the leaderboard, but I think you're having troubles actually setting up the leaderboard? Yes? From iTunes Connect goto your apps, from there goto the app you want the leaderboard in, and from there goto game centre. Click add leaderboard, and choose a single leaderboard, and then fill out the information. Your leaderboard ID is the name you put in your code e.g. i've called mine LeaderBoard name up above. Once you've done that, you then go back to your app and scroll down until you find game centre, and then click the plus sign and add the selected leaderboard. However if you don't know how to even add an app to iTunes connect, you should seriously be reading whole articles, example here's a good place to find out how. And here's a good place to look on how to understand game centre.

Upvotes: 2

Related Questions