Advertisement
priore

Fix GameCenter on Cocos2D v2 and iOS 6

Feb 15th, 2013
921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // How to fix GameCenter Leaderboard in Landscape mode
  2. // with Cocos2d v.2.0 and iOS6.
  3. //
  4. // Author: Danilo Priore
  5. // http://www.prioregroup.com
  6. //
  7. // 1. Create a personalized UINavigationController
  8. //
  9. // GameNavigationController.h
  10. @interface GameNavigationController : UINavigationController
  11. @end
  12.  
  13. // GameNavigatioController.m
  14. #import "GameNavigationController.h"
  15. @implementation GameNavigationController
  16. - (BOOL)shouldAutorotate {
  17.     return YES;
  18. }
  19. - (NSUInteger)supportedInterfaceOrientations {
  20.     return UIInterfaceOrientationMaskLandscape;
  21. }
  22. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
  23.     return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
  24. }
  25. - (BOOL)isNavigationBarHidden {
  26.     return YES;
  27. }
  28. @end
  29.  
  30. //
  31. // 2. import the GameNavigationController in AppDelegate and change interface declaration
  32. //
  33. #import "GameNavigationController.h"
  34. @interface AppController : NSObject <UIApplicationDelegate, CCDirectorDelegate>
  35. {
  36.     UIWindow *window_;
  37.     GameNavigationController *navController_;
  38.     CCDirectorIOS   *director_;
  39. }
  40. @property (nonatomic, retain) UIWindow *window;
  41. @property (nonatomic, readonly) GameNavigationController *navController;
  42. @property (readonly) CCDirectorIOS *director;
  43. @end
  44.  
  45. //
  46. // 3. Change the application:didFinishLaunchingWithOptions: delegate in AppController.mm
  47. // from where it begins "navController_ = [[UINavigationController alloc] initWithRootViewController:director_];" (including this) with the code below:
  48. //
  49. // Create a Navigation Controller with the Director
  50. navController_ = [[GameNavigationController alloc] initWithRootViewController:director_];
  51. // set the Navigation Controller as the root view controller
  52. NSString *reqSysVer = @"6.0";
  53. NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
  54. if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending) {
  55.     [window_ addSubview:navController_.view];
  56.         [window_ setRootViewController:navController_];
  57. } else {
  58.         [window_ addSubview:navController_.view];
  59. }
  60. // make main window visible
  61. [window_ makeKeyAndVisible];
  62. // Run the scene.
  63. [director_ runWithScene:[GameMenu node]];
  64. // etc. etc.
  65.  
  66. //
  67. // 4. In your main scene import AppDelegate.h, specifies that you want to use
  68. // the delegate of GK and declare a generic UIViewController pointer.
  69. //
  70. #import <GameKit/GameKit.h>
  71. @interface YourMainScene : CCLayer <GKLeaderboardViewControllerDelegate>
  72. {
  73.     UIViewController *controllerContainer;
  74. }
  75. @end
  76.  
  77. #import "AppDelegate.h"
  78. @implementation YourMainScene
  79. #pragma mark - GKLeaderboardViewController
  80. // Leaderboard show scores method
  81. - (void)showScores
  82. {
  83.     GKLeaderboardViewController *leaderboardController = [[[GKLeaderboardViewController alloc] init] autorelease];
  84.     if (leaderboardController != NULL) {
  85.         leaderboardController.leaderboardDelegate = self;
  86.         leaderboardController.timeScope = GKLeaderboardTimeScopeToday;
  87.         leaderboardController.toolbar.autoresizesSubviews = YES;
  88.         leaderboardController.view.bounds = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
  89.         if (controllerContainer == nil) {
  90.             NSObject<UIApplicationDelegate> *universalAppDelegate = (NSObject<UIApplicationDelegate>*)[[UIApplication sharedApplication] delegate];
  91.             controllerContainer = [(AppController*)universalAppDelegate navController];
  92.         }
  93.         [controllerContainer presentModalViewController:leaderboardController animated:YES];
  94.     }
  95. }
  96. #pragma mark - GKLeaderboardViewController Delegate
  97. - (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
  98. {
  99.         [controllerContainer dismissModalViewControllerAnimated:YES];
  100.     [self callDelegateOnMainThread:@selector(leaderboardViewControllerDidFinish:) withArg:viewController error:NULL];
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement