I have managed to get my game to sign the player into game center on launch however when a highscore is reached it just saves it on the game rather than sending it to game centre. Wherever i put the
[self reportScore];
it seem to make the simulator crash, I have attached my view controller.m file if you could help me with a method to make my game send the highscore to Game Center so i can move on to posting the leaderboard on to displaying the leaderboard in the app. I am following this tutorial by the way http://www.appcoda.com/ios-game-kit-framework/
#import "ViewController.h"
#import <GameKit/GameKit.h>
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
@interface ViewController ()
@property (nonatomic, strong) NSString *leaderboardIdentifier;
@property (nonatomic,assign) BOOL gameCenterEnabled;
-(void)authenticateLocalPlayer;
-(void)reportScore;
@end
@implementation ViewController
-(void)reportScore{
GKScore *score = [[GKScore alloc] initWithLeaderboardIdentifier:_leaderboardIdentifier];
score.value = HighScoreNumber;
[GKScore reportScores:@[score] withCompletionHandler:^(NSError *error) {
if (error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
}];
}
-(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)viewDidLoad
{
[self authenticateLocalPlayer];
HighScoreNumber = [[NSUserDefaults standardUserDefaults] integerForKey:@"HighScoreSaved"];
HighScore.text = [NSString stringWithFormat:@"High Score: %li", (long)HighScoreNumber];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark iAd Delegate Methods
-(void)bannerViewDidLoadAd:(ADBannerView *)banner {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:1];
[UIView commitAnimations];
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[banner setAlpha:0];
[UIView commitAnimations];
}
@end
Can you try whether this simple snippet works for you? _localPlayer
refers to an instance variable which is set with the authentication handler.
- (IBAction)doAddAScore:(id)sender {
GKLocalPlayer *lp = [GKLocalPlayer localPlayer];
NSInteger score = 100;
if (lp && lp.isAuthenticated) {
NSString *lbid = @"your.leaderboard.id";
GKScore *gkScore = [[GKScore alloc] initWithLeaderboardIdentifier:lbid player:lp];
gkScore.value = score;
[GKScore reportScores:@[gkScore] withCompletionHandler:^(NSError * _Nullable error) {
NSLog(@"GKScore::reportScores completed - error : %@", error);
}];
} else {
NSLog(@"reporting score: localPlayer nil or not authenticated");
}
}
This code is from a test prototype of mine which I use for resolving game invites and the score submission just worked. There is no other part of the code which deals with score submission.