Is there some straightforward documentation on implementing the Game Center?

502 Views Asked by At

I'm trying to implement Game Center into my ios7 game (Xcode 5), but the material in the apple docs and the stuff I've seen online doesn't seem to work very well.

These are the two main methods I'm using wish produce no errors but I don't get any data either:

- (void) retrieveTopTenScores 
 {
  GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];
  if (leaderboardRequest != nil)
   {
    leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal;
    leaderboardRequest.timeScope = GKLeaderboardTimeScopeToday;
    leaderboardRequest.identifier = kLeaderboardID;
    leaderboardRequest.range = NSMakeRange(1,10);
    [leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
        if (error != nil)
        {
            // Handle the error.
        }
        if (scores != nil)
        {                
          // Process the score information. 
         } else {
           NSLog(@"scores retrieved successfully but no scores in the leaderboard"); 
        }
    }];
  }
}



-(void)submitMyScore
{
 //This is the same category id you set in your itunes connect GameCenter LeaderBoard
 GKScore *myScoreValue = [[GKScore alloc] initWithLeaderboardIdentifier:kLeaderboardID];
 myScoreValue.value = 5123123;

 [myScoreValue reportScoreWithCompletionHandler:^(NSError *error){
    if(error != nil){
        NSLog(@"Score Submission Failed");
    } else {
        NSLog(@"Score Submitted");
    } 
 }];
}

So I'm looking for some simple example code to do this successfully... thanks rich

2

There are 2 best solutions below

1
On

I see nothing wrong with your code. Is the player authenticated when you run it?, what error are you getting? If you look for sample GameKit code there is some at iOS 6 Advanced Cookbook from Erica Sadun, but nothing you shouldn't be able to figure out reading the API.

0
On

the answer is this for submitting scores in iOS7 to the game centre

Game Center Helper/Manager/Control (Object).h

 + (gamecenterhelper/manager/control *)sharedInstance;
 -(void)reportScore:(int64_t)score forLeaderboardID:(NSString*)identifier;

Game Center Helper/Manager/Control (Object).m

-(void)reportScore:(int64_t)score forLeaderboardID:(NSString*)identifier
{
GKScore *scoreReporter = [[GKScore alloc] initWithLeaderboardIdentifier: identifier];
scoreReporter.value = score;
scoreReporter.context = 0;

NSArray *scores = @[scoreReporter];
[GKScore reportScores:scores withCompletionHandler:^(NSError *error) {

}];
}

viewcontroller.h

#import "gamecenterhelper/manager/control"

viewcontroller.m

[[gamecenterhelper/manager/control sharedInstance] reportScore:(int64_t) forLeaderboardID:(NSString*)];

//in place of int64_t place your integer you want uploaded, and instead on NNString* add your leaderboard identifier