Add Game Center Objective C Sprite Kit report Scores - objective-c

I have made a helper singleton Class to implement Game Center. Then I am calling the method to authenticate the player in my AppDelegate.
If I start the app, all good!
Next step: I want to report my scores. I have this method as report Score
+ (void) reportScore: (Float64) score forIdentifier: (NSString*) identifier
{
GKScore* highScore = [[GKScore alloc] initWithLeaderboardIdentifier:identifier];
highScore.value = score;
[GKScore reportScores:#[highScore] withCompletionHandler:^(NSError *error) {
if (error) {
NSLog(#"Error in reporting scores: %#", error);
}
}];
}
I call it when I have GameOver and shortly before switching to my GameOverScene:
[GCTurnBasedMatchHelperr reportScore:self.scoring forIdentifier:#"score"];
I am playing with a Sandbox Test User. If I go to game center then to the leaderboard of my game, it says: No Scores.
After that, I would like to display the leaderboard and the scores somehow.
If you need any more code, go to this github: https://gist.github.com/anonymous/1881303471cb9790af0b
Can anyone help me out?

Okay, I did a really simple mistake, I just used a wrong identifier when I report the score. It is not score it was Score.. .

Related

Game Center Leaderboard score upload failed in iOS 7.0

I've much googling because I've issue in implementing Game Center Leaderboard in iOS 7.. So I've got one important thing that is likely for solving the issue. From iOS 7.0, we should use identifier instead of category because category is deprecated from iOS 7.0.. I could get this fact from here. https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/GameKit_Guide/LeaderBoards/LeaderBoards.html#//apple_ref/doc/uid/TP40008304-CH6-SW13
So I used identifier ... but it still faces with same issue... Score upload fails..
Belows are my code for reporting score.
GKScore *scoreReporter = [[GKScore alloc] initWithLeaderboardIdentifier: identifier];
scoreReporter.value = score;
scoreReporter.context = 0;
scoreReporter.shouldSetDefaultLeaderboard = YES;
NSArray *scores = #[scoreReporter];
[GKScore reportScores:scores withCompletionHandler:^(NSError *error) {
//Do something interesting here.
[self callDelegateOnMainThread: #selector(scoreReported:) withArg: NULL error: error];
}];
When reports score, it shows error and error is "The requested operations could not be completed because one or more parameters are invalid."
Any solution for it? I've been almost devoting 2 days for it but not getting anything funny.. Could anyone give me a solution? Need any helps .. Thanks..
I solved my issue by removing the line
scoreReporter.shouldSetDefaultLeaderboard = YES;
:) Hope this helps other developers who are facing with the same issue as I was...

Reporting score to gamecenter for ios7

According to https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/LeaderBoards/LeaderBoards.html
Reporting score to gamecenter in ios7 should be done using
[GKLeaderboard reportScores:scores withCompletionHandler:^(NSError *error) {
//Do something interesting here.
}];
However, I could not find any reference to this method in GKLeaderboard.
The method does not exist here:
https://developer.apple.com/library/ios/documentation/GameKit/Reference/GKLeaderboard_Ref/Reference/Reference.html
GKLeaderboard.h does not contain a reportScores method also.
The former way of reporting score using GKScore's reportScoreWithCompletionHandler method had been deprecated so I am reluctant to use that.
Does anyone know whats the correct way to report score to gamecenter in ios7?
I can confirm that the reportScores:withCompletionHandler: method does work; I'm using it in one of my apps. It's located in the header file GKScore.h. This is how I'm using it:
- (void) reportHighScore:(NSInteger) highScore {
if ([GKLocalPlayer localPlayer].isAuthenticated) {
GKScore* score = [[GKScore alloc] initWithLeaderboardIdentifier:MY_LEADERBOARD_ID];
score.value = highScore;
[GKScore reportScores:#[score] withCompletionHandler:^(NSError *error) {
if (error) {
// handle error
}
}];
}
}

Block do not execute the way i want, lack of understanding

I am trying to implement Game Center into my app and the code below is reporting the score.
I am trying to accomplish a solution were if the player is not authenticated it should save the score in scoreDictionary via the scoreReporter block. However, when the player is not authenticated it never hits the "If (error != nil)" statement.
As a matter of fact it passes the whole block. If the local player is authenticated it execute the block.
This is the first time i look into both Game Center and blocks in detail so i am a bit lost here.
What i want to accomplish is as described above.
I use 5.1 as target.
-(void)reportScore:(int64_t)score forCategory:(NSString*)category {
NSLog(#"reportScore: %lli", score);
NSLog(#"category: %#",category);
GKScore *scoreReporter = [[GKScore alloc] initWithCategory:category];
scoreReporter.value = score;
NSLog(#"scoreReporter: %#", scoreReporter);
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
NSLog(#"Execute the scoreReporter the block");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *scoreFilePath = [NSString stringWithFormat:#"%#/scores.plist",[paths
objectAtIndex:0]];
NSMutableDictionary *scoreDictionary=[NSMutableDictionary
dictionaryWithContentsOfFile:scoreFilePath];
if (error != nil)
{
//There was an error so we need to save the score locally and resubmit later
NSLog(#"Saving score for later");
[scoreDictionary setValue:scoreReporter forKey:[NSDate date]];
[scoreDictionary writeToFile:scoreFilePath atomically:YES];
}
}];
}
NSLog output when the player is not authenticated:
reportScore: 80
category: com.xxxxxx.yyyyyyyHighScore
scoreReporter: <GKScore: 0xab611b0>player:(null) rank:0 date:2012-12-19 11:18:04 +0000 value:80 formattedValue:(null) context:0x0
NSLog output when the player is authenticated:
reportScore: 60
category: com.xxxxxx.yyyyyyyHighScore
scoreReporter: <GKScore: 0x964ce30>player:G:280817155 rank:0 date:2012-12-19 11:27:45 +0000 value:60 formattedValue:(null) context:0x0
Execute the scoreReporter the block
The Game Center Programming Guide says "Most Game Center classes function only if there is an authenticated player, and those classes implicitly reference the local player. For example, when your game reports scores to a leaderboard, it always reports scores for the local player. Your game must disable all Game Center features when there is not an authenticated player."
Probably what is going on is that reportScoreWithCompletionHandler: doesn't run at all, so it doesn't "complete", so it doesn't call the completionHandler. Test for an authenticated player before reportScoreWithCompletionHandler: GameKit code typically has several occurrences of the following if/else:
if ( [GKLocalPlayer localPlayer].authenticated ) {
...
} else {
...
}

iOS 6 game Center

previously to me upgrading to iOS 6, I had gamecenter working as it should. Now I cannot seem to pass achievements to the gamecenter. The achievement happens, notifies the user with an alert but says "not achieved" in the game center. I have made a sandbox account and the app has not yet been reviewed.
This is how I was previously sending an achievement to game center:
[gameCenterManager submitAchievement:#"shareFacebook" percentComplete:100];
Is this not right anymore? I previously implemented this from the tutorial by mobile.tutsplus.com
Thanks in advance
This is how I'm doing it and seems to work well enough in iOS 6:
- (void) reportAchievementIdentifier: (NSString*) identifier percentComplete: (float) percent
{
GKAchievement *achievement = [[GKAchievement alloc] initWithIdentifier: identifier];
if (achievement)
{
achievement.percentComplete = percent;
[achievement reportAchievementWithCompletionHandler:^(NSError *error)
{
if (error != nil)
{
NSLog(#"Error in reporting achievements: %#", error);
}
}];
}
}
Hope this helps!

Game Center: authenticates ok, shows leaderboard ok, posts (my) score ok, only 2 players?

My game (called "Clear the Square") has gotten approved. It's getting hundreds of downloads. It's currently #44 in free word games in the US. There is only one problem: when I go to the Game Center leaderboards, it shows two users, one of them being me. And one of them being (very appropriately named) someone who isn't me. Here is a screenshot of what this looks like: http://clearthesquareapp.com/photo-9.PNG
I know that there are more than two people playing this (iAd tells me so.) I also know that the game is not unwinnable; somewhat challenging, but not unwinnable. And I know that Game Center didn't just completely go out of style overnight. In all other games that have ever been anywhere near a top-100 list, there are at least a few hundred all-time scores on the leaderboard.
Thinking that the problem might be that my development build is somehow special and goes to an alternate, "sandbox" set of leaderboards, I deleted the binary from my phone, rebooted my phone, and downloaded the game from the App Store. Same thing. So that wasn't the problem.
Here's all my GameCenter code; it comes straight from example projects. I don't know what could be missing, since it does successfully authenticate, it does show leaderboards, and it does post my score -- each time I set a high score, it is instantly reflected in the leaderboard.
I would really, really appreciate it if someone could download the free version of my game, and send me a screenshot of what the leaderboards look like on your end. You would definitely get promo codes for every future app I make.
- (void) authenticateLocalPlayer
{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
[localPlayer authenticateWithCompletionHandler:^(NSError *error) {
if (localPlayer.isAuthenticated)
{
NSLog(#"authenticated in game center!");
// Perform additional tasks for the authenticated player.
}
}];
}
- (void) reportScore: (int64_t) score forCategory: (NSString*) category
{
GKScore *scoreReporter = [[[GKScore alloc] initWithCategory:category] autorelease];
scoreReporter.value = score;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
if (error != nil)
{
// handle the reporting error
}
}];
}
- (void) showLeaderboard
{
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != nil)
{
leaderboardController.leaderboardDelegate = self;
[self presentModalViewController: leaderboardController animated: YES];
}
}
- (void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController {
[self dismissModalViewControllerAnimated:YES];
}
There should be more than two players on this leaderboard. What am I missing?
Edit: also, worth noting that it has already been out for about 48 hours, and probably has about 1000 downloads so far. So I am quite sure that it's not just a matter of nobody having had time to win a game yet.
You're probably still signed in to your "sandbox" Game Center account. Log out of that, and then log into your real account, and you should see the real leaderboards.
I ran into this problem myself, and it sometimes didn't "take" to log out and back in, so try a couple times. I think I may have had to kill (via the double-tap home button) the Game Center app after logging out of the sandbox, and before logging into the real account, for it to work.