1

I'm trying to add the score at the end of the game to Game Center but it's adding nothing to the game center. I'm searching all day long for the 'mistake' i made but i do not see anything?

Here is a little bit of the code i us.

.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <GameKit/GameKit.h>

@class GKLeaderboard, GKAchievement, GKPlayer;

int counter;

@protocol TopscoreDelegate <NSObject>
@optional
- (void) processGameCenterAuth: (NSError*) error;
- (void) scoreReported: (NSError*) error;
- (void) reloadScoresComplete: (GKLeaderboard*) leaderBoard error: (NSError*) error;
@end

@interface Topscore : UIViewController <NSObject> {

    AVAudioPlayer *audioPlayer;
    IBOutlet UILabel *count;
    Topscore* Topscore;
}

- (IBAction)tweetTapped:(id)sender;
- (IBAction) subScore;
- (IBAction) showLeader;
- (void) reportScore;

@property (nonatomic, assign)  id <TopscoreDelegate> delegate;

+ (BOOL) isGameCenterAvailable;
- (void) authenticateLocalUser;

@property (nonatomic, retain) Topscore *Topscore;
@property (nonatomic, retain) NSString* currentLeaderBoard;

@property (nonatomic, retain) UILabel *count;
@property(nonatomic, readonly, retain) NSDate *date;

- (void) reportScore: (int64_t) score forCategory: (NSString*) category;
- (void) reloadHighScoresForCategory: (NSString*) category;

@end

And the .m file looks like; i have a button in the xib file wich is connected to the IBAction Subscore. i'm not receiving errors but only a few warnings.

PerformSelector may cause a leak because its selector is unknown

This warning on line;

[delegate performSelector: selector withObject: arg withObject: err];

.m

@interface Topscore ()

@property (strong, nonatomic) NSString *imageString;
@property (strong, nonatomic) NSString *urlString;
- (void)clearLabels;
@end

@implementation Topscore

@synthesize count, date, urlString = _urlString, imageString = _imageString, delegate, Topscore, currentLeaderBoard;

- (void) callDelegate: (SEL) selector withArg: (id) arg error: (NSError*) err
{
    assert([NSThread isMainThread]);
    if([delegate respondsToSelector: selector])
    {
        if(arg != NULL)
        {
            [delegate performSelector: selector withObject: arg withObject: err];
        }
        else
        {
            [delegate performSelector: selector withObject: err];
        }
    }
    else
    {
        NSLog(@"Missed Method");
    }
}

- (void) callDelegateOnMainThread: (SEL) selector withArg: (id) arg error: (NSError*) err
{
    dispatch_async(dispatch_get_main_queue(), ^(void)
                   {
                       [self callDelegate: selector withArg: arg error: err];
                   });
}

//..

+ (BOOL) isGameCenterAvailable
{
    // check for presence of GKLocalPlayer API
    Class gcClass = (NSClassFromString(@"GKLocalPlayer"));

    // check if the device is running iOS 4.1 or later
    NSString *reqSysVer = @"4.1";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);

    return (gcClass && osVersionSupported);
}

- (void) authenticateLocalUser
{
    if([GKLocalPlayer localPlayer].authenticated == NO)
    {
        [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) 
         {
             [self callDelegateOnMainThread: @selector(processGameCenterAuth:) withArg: NULL error: error];
         }];
    }
}

- (void) reloadHighScoresForCategory: (NSString*) category
{
    GKLeaderboard* leaderBoard= [[GKLeaderboard alloc] init];
    leaderBoard.category= category;
    leaderBoard.timeScope= GKLeaderboardTimeScopeAllTime;
    leaderBoard.range= NSMakeRange(1, 1);

    [leaderBoard loadScoresWithCompletionHandler:  ^(NSArray *scores, NSError *error)
     {
         [self callDelegateOnMainThread: @selector(reloadScoresComplete:error:) withArg: leaderBoard error: error];
     }];
}

//..

- (void) showAlertWithTitle: (NSString*) title message: (NSString*) message
{
    UIAlertView* alert= [[UIAlertView alloc] initWithTitle: title message: message 
                                                  delegate: NULL cancelButtonTitle: @"OK" otherButtonTitles: NULL];
    [alert show];

}

- (void) scoreReported: (NSError*) error;
{
    if(error == NULL)
    {
        [self.Topscore reloadHighScoresForCategory: self.currentLeaderBoard];
        [self showAlertWithTitle: @"High Score Reported!"
                         message: [NSString stringWithFormat: @"", [error localizedDescription]]];
    }
    else
    {
        [self showAlertWithTitle: @"Score Report Failed!"
                         message: [NSString stringWithFormat: @"Reason: %@", [error localizedDescription]]];
    }
}

- (IBAction) subScore {

    GKScore *scoreReporter = [[GKScore alloc] initWithCategory:@"01"];
   scoreReporter.value = counter;


    [scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
        if (error != nil)
        {
            NSLog(@"Failed: %i", counter);   
        } else {
            NSLog(@"Succes: %i", counter);  


            [self callDelegateOnMainThread: @selector(scoreReported:) withArg: NULL error: error];

        }
    }];

}

Please if u need more code just ask me!

PS: The leaderbord is showing up but the scores posting/adding not adding after clicking on the button (IBAction)

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
Blazer
  • 14,259
  • 3
  • 30
  • 53
  • 4
    Start a recurrent timer the first time the button's pressed. Have the timer delegate update the time display. You'll need a flag in your button handler routine to know that you've started the timer, so you know to count presses vs starting the timer. When the time expires, disable the button. – Hot Licks Jan 04 '12 at 19:01
  • Not the answer i was looking for..? – Blazer Jan 07 '12 at 23:21
  • What is with **`[delegate performSelector: selector withObject: arg withObject: err];`**. Why would you need 2 **withObject**'s? – WrightsCS Jan 08 '12 at 04:23
  • That's not the question I answered. – Hot Licks Jan 08 '12 at 04:24
  • Game Center 1 - Nathan 0 – Derek Jan 12 '12 at 14:36

1 Answers1

3

I answered a similar question in regards to Game Center Scores not showing up.

You need atleast a minimum of two score posted one from different accounts: Solution

The same should be with asking for scores from GC as it won't return any score if there is only 1 person the on the board.

Community
  • 1
  • 1
CStreel
  • 2,642
  • 2
  • 19
  • 37
  • Thanks but i tried 3/4 accounts and nothing is showing? On my iPad/iPhone – Blazer Jan 08 '12 at 18:01
  • put NSLog from the completion handler through to the selector method called when leaderboards are loaded and put them into your question. It may help to track down the problem. Edit:it doesn't seem to with your Topscore Interface – CStreel Jan 08 '12 at 21:48
  • I posted with 2 users. 3 posts from each user.... still no sign of scores in game center. – Satyam Jan 17 '13 at 16:27