Sunday, August 19, 2012

Game Center Leaderboard Dismissal on iPad vs. iPhone

A random piece of knowledge I send off to you for whom it may help.

I used Cocos2d and Ganbaru Game's Game Center helper to integrate Game Center into Dead End, an iOS game of mine.

However, upon converting the game to Universal, I found two problems.

1. GameCenter wouldn't pop up on iPad.
To resolve this, you have to change the modalPresentationStyle for your leaderboard.  The default does not work on iPad.

So this is my showLeaderboard code, with an added line for iPad/iPhone integration.
- (void)showLeaderboardForCategory:(NSString *)category
{
    if (hasGameCenter)
    {
        GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
        if (leaderboardController != nil)
        {
            myViewController = [[UIViewController alloc] init];
            
            leaderboardController.leaderboardDelegate = self;
            leaderboardController.category = category;
            leaderboardController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

            if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
                leaderboardController.modalPresentationStyle = UIModalPresentationFullScreen;
            else
                leaderboardController.modalPresentationStyle = UIModalPresentationFormSheet;
            
            [[[[CCDirector sharedDirector] openGLView] windowaddSubview:myViewController.view];
            [myViewController presentModalViewController:leaderboardController animated:YES];
        }
        [leaderboardController release];
    }
}

2. When I was dismissing the leaderboard on iPad, touch controls stopped working.

On iPhone/iPod, the dismissal code looked like:
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
    [myViewController dismissModalViewControllerAnimated:YES];
    [myViewController.view.superview removeFromSuperview];//I added this to Ganbaru's code
    [myViewController release];
}

On iPad, however, instead of [myViewController.view.superview removeFromSuperview], it should only be [myViewController.view removeFromSuperview].  I'm not sure why, but it works for me.

Thus, to get leaderboard dismissal to work on both iPhone AND iPad, the full code should be:
[code]
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
    [myViewController dismissModalViewControllerAnimated:YES];

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        [myViewController.view.superview removeFromSuperview];
    else
        [myViewController.view removeFromSuperview];
    
    [myViewController release];
}

Hope this is useful for someone!

No comments: