Created
October 25, 2013 23:49
-
-
Save priore/7163577 to your computer and use it in GitHub Desktop.
Fix GameCenter on Cocos2D v2 and iOS 6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment