Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

posermobile89

macrumors newbie
Original poster
Jul 21, 2011
1
0
Hello all,

I am trying to integrate Apple's game center into my application. I can successfully post scores to the leader board, and show the leader board, but the problem comes when I try to dismiss the leader board modal view. I've followed apple's code direction from the Game Kit Programming Guide (http://developer.apple.com/library/.../GameKit_Guide/LeaderBoards/LeaderBoards.html).

My code is as follows for Game Center:

Code:
-(BOOL)isGameCenterAvailable{
    // Check for presence of GKLocalPlayer class.
    BOOL localPlayerClassAvailable = (NSClassFromString(@"GKLocalPlayer")) != nil;
    
    // The device must be running iOS 4.1 or later.
    NSString *reqSysVer = @"4.1";
    NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
    BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);
    return (localPlayerClassAvailable && osVersionSupported);
}

- (void) authenticateLocalPlayer
{
    if([self isGameCenterAvailable]){
        GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
        [localPlayer authenticateWithCompletionHandler:^(NSError *error) {
            if (localPlayer.isAuthenticated)
            {
                // 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];
    }
    //[leaderboardController release];
}

- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
    
    if([self modalViewController] != nil){
        [self dismissModalViewControllerAnimated:YES];
    }
     
}

-(IBAction)show{
    [self showLeaderboard];
}

-(IBAction)submit{
    [self reportScore:9 forCategory:kLeaderboardID];
}

Xcode tells me the problem line is [self dismissModalViewControllerAnimated:YES]; it says I'm getting bad access, which I know means I'm trying to access a bad pointer, but I don't see why anything wouldn't be invalid. Self reports that it has a modalviewcontroller. I've tried all sorts of variants, and I'm completely baffled as to why it is giving me errors.

Any help or suggestions would be greatly appreciated.

Thanks in advance!
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.