I'm creating a game with two View Controllers and I'm trying to implement GameCenter to my game. I'm authenticating local player in Second View Controller, and there is no such coding for game center in my "first view controller', 'Play game' button on the "First View Controller" is linked by action segue 'modal' to the second view controller, and 'Exit' button on the "Second View Controller" takes the game back to "First View Controller".
Now the problem is that whenever I play the game and make a high score my score is reported perfectly to game center leaderboard , but every time i switch between the two view controllers using 'Play Game' or 'Exit' button, no more score is reported to game center even if i make highscore, I am stuck here and i cant find my mistake, If i terminate my game with spring board and close it, same happens next time I start game and score is only reported first time but no score reported to game center when I move between the two view controllers.
Here is my SecondViewController.m file coding
@interface SecondViewController ()
@property (nonatomic) int64_t score;
@property (nonatomic) BOOL gameCenterEnabled;
@property (nonatomic, strong) NSString *leaderboardIdentifier;
-(void)reportScore;
-(void)authenticateLocalPlayer;
-(void)showLeaderboardAndAchievements:(BOOL)shouldShowLeaderboard;
@end
@implementation SecondViewController
-(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;
// Get the default leaderboard identifier.
[[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) {
if (error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
else{
_leaderboardIdentifier = leaderboardIdentifier;
}
}];
}
else{
_gameCenterEnabled = NO;
}
}
};
}
-(void)showLeaderboardAndAchievements:(BOOL)shouldShowLeaderboard {
GKGameCenterViewController *gcViewController = [[GKGameCenterViewController alloc] init];
gcViewController.gameCenterDelegate = self;
if (shouldShowLeaderboard) {
gcViewController.viewState = GKGameCenterViewControllerStateLeaderboards;
gcViewController.leaderboardIdentifier = _leaderboardIdentifier;
}
else {
gcViewController.viewState = GKGameCenterViewControllerStateAchievements;
}
[self presentViewController:gcViewController animated:YES completion:nil];
}
-(void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController {
[gameCenterViewController dismissViewControllerAnimated:YES completion:nil];
}
-(void)reportScore {
GKScore *score = [[GKScore alloc] initWithLeaderboardIdentifier:_leaderboardIdentifier];
score.value = _score;
[GKScore reportScores:@[score] withCompletionHandler:^(NSError *error) {
if (error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
}];
}
These two buttons for reporting score and viewing leaderboard
- (IBAction)HandleReportScore:(id)sender {
[self reportScore];
}
- (IBAction)LabelShowLeaderboard:(id)sender {
[self showLeaderboardAndAchievements:YES];
}
My score method is this and I call this method when i need to increase score in my game
-(void)Score {
_score += 20;
}
In view did load I am authenticating the local player
- (void)viewDidLoad {
[super viewDidLoad];
[self authenticateLocalPlayer];
[self initValues];
_gameCenterEnabled = NO;
_leaderboardIdentifier = @"";
}
And here is the initial values method for score which I called in view did load
-(void)initValues{
// Set the initial values to all member variables.
_score = 0;
}
@end
No errors show up on build, but the score isn't reported. How can I fix this?