whitebear
whitebear

Reputation: 12461

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!

Upvotes: 0

Views: 207

Answers (2)

Yossi
Yossi

Reputation: 2535

It should look like that:

[[CCDirector sharedDirector] presentViewController: leaderboardController animated: YES completion:nil];

The complete code:

GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
if (leaderboardController != NULL)
{
    leaderboardController.category = kLeaderboardID;
    leaderboardController.timeScope = GKLeaderboardTimeScopeWeek;
    leaderboardController.leaderboardDelegate = self;
    [[CCDirector sharedDirector] presentViewController:leaderboardController animated:YES completion:nil];
}

Upvotes: 1

Luca Angeletti
Luca Angeletti

Reputation: 59536

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];

Upvotes: 1

Related Questions