Objective-C Tips & Trick - Prioregroup.com
[Objective-C] Fix GameCenter on Cocos2D v2 and iOS 6
       
back

// How to fix GameCenter Leaderboard in Landscape mode
// with Cocos2d v.2.0 and iOS6.
//
// Author: Danilo Priore
// http://www.prioregroup.com
//
// 1. Create a personalized UINavigationController
//
// GameNavigationController.h
@interface GameNavigationController : UINavigationController
@end

// GameNavigatioController.m
#import "GameNavigationController.h"
@implementation GameNavigationController
- (BOOL)shouldAutorotate {
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
- (BOOL)isNavigationBarHidden {
    return YES;
}
@end

//
// 2. import the GameNavigationController in AppDelegate and change interface declaration
//
#import "GameNavigationController.h"
@interface AppController : NSObject <UIApplicationDelegate, CCDirectorDelegate>
{
	UIWindow *window_;
	GameNavigationController *navController_;
	CCDirectorIOS	*director_;
}
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, readonly) GameNavigationController *navController;
@property (readonly) CCDirectorIOS *director;
@end

//
// 3. Change the application:didFinishLaunchingWithOptions: delegate in AppController.mm
// from where it begins "navController_ = [[UINavigationController alloc] initWithRootViewController:director_];" (including this) with the code below:
//
// Create a Navigation Controller with the Director
navController_ = [[GameNavigationController alloc] initWithRootViewController:director_];
// set the Navigation Controller as the root view controller
NSString *reqSysVer = @"6.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {
	[window_ addSubview:navController_.view];
        [window_ setRootViewController:navController_];
} else {
        [window_ addSubview:navController_.view];
}
// make main window visible
[window_ makeKeyAndVisible];
// Run the scene.
[director_ runWithScene:[GameMenu node]];
// etc. etc.

//
// 4. In your main scene import AppDelegate.h, specifies that you want to use 
// the delegate of GK and declare a generic UIViewController pointer.
//
#import <GameKit/GameKit.h>
@interface YourMainScene : CCLayer <GKLeaderboardViewControllerDelegate>
{
    UIViewController *controllerContainer;
}
@end

#import "AppDelegate.h"
@implementation YourMainScene
#pragma mark - GKLeaderboardViewController
// Leaderboard show scores method
- (void)showScores 
{
    GKLeaderboardViewController *leaderboardController = [[[GKLeaderboardViewController alloc] init] autorelease];
    if (leaderboardController != NULL) {
        leaderboardController.leaderboardDelegate = self;
        leaderboardController.timeScope = GKLeaderboardTimeScopeToday;
        leaderboardController.toolbar.autoresizesSubviews = YES;
        leaderboardController.view.bounds = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
        if (controllerContainer == nil) {
            NSObject<UIApplicationDelegate> *universalAppDelegate = (NSObject<UIApplicationDelegate>*)[[UIApplication sharedApplication] delegate];
            controllerContainer = [(AppController*)universalAppDelegate navController];
        }
        [controllerContainer presentModalViewController:leaderboardController animated:YES];
    }
}
#pragma mark - GKLeaderboardViewController Delegate
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController 
{
    	[controllerContainer dismissModalViewControllerAnimated:YES];
	[self callDelegateOnMainThread:@selector(leaderboardViewControllerDidFinish:) withArg:viewController error:NULL];
}






Copyright © 1996-2018 Centro Studi Informatica di Danilo Priore. All rights reserved. P.I.10149810581.
prioregroup.com