Search code examples
objective-ccocos2d-iphonegame-centerspritebuilder

Cocos2d cocos builder(spritebuilder) with game center


I would like to show UIView for game center.

-(void)authenticateLocalPlayer {
    [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
        if (error)

        else

    }];
}
- (void)didLoadFromCCB {

    [self authenticateLocalPlayer];

    _viewController = [[UIViewController alloc] init];

    GKLeaderboardViewController* leaderboardController = [[GKLeaderboardViewController alloc] init];
    if (leaderboardController != nil){

        leaderboardController.leaderboardDelegate = self;
        leaderboardController.timeScope = GKLeaderboardTimeScopeAllTime; 
        leaderboardController.category = @"mygamehighscore";
        [_viewController presentViewController: leaderboardController animated: YES completion:nil];
    }
    [[[CCDirector sharedDirector] view] addSubview:_viewController.view];

}

but it shows this error and no game center pop up appeared.

Warning: Attempt to present <GKLeaderboardViewController: 0x15db51f0> on <UIViewController: 0x15db4d10> whose view is not in the window hierarchy!

Solution

  • I think it's because you are invoking the method presentViewController on the wrong view controller (which is not in the hierarchy when the method is invoked).

    There is no need to create a new UIViewController, you can use the one provided by Cocos2D (my code refers to Cocos2D-iPhone 2.0).

    Try with:

    AppController * appController = (AppController*) [[UIApplication sharedApplication] delegate];
    [appController.navController presentViewController: leaderboardController animated:YES completion:nil];