News

Hot fix of Orientation Problem on iOS 6

By walzer Posted 2012-09-23 14:28 Comments 6

Since iOS updated some orientation relatived methods, cocos2d-x games with v2.0.2 and before can’t be rendered to correct orientation.

I fixed it in this commit https://github.com/walzer/cocos2d-x/commit/70f1360ac2f0397ae8e32422a3be1d232bebd410
You can update your games simply with the codes below:

  1. yougame/ios/AppController.mm. Change this line

    // Set RootViewController to window
    [window addSubview: viewController.view];

to

// Set RootViewController to window
if ( [[UIDevice currentDevice].systemVersion floatValue] < 6.0)
{
    // warning: addSubView doesn't work on iOS6
    [window addSubview: viewController.view];
}
else
{
    // use this mehod on ios6
    [window setRootViewController:viewController];
}
  1. In yourgame/ios/RootViewController.mm, we used this method to deal with auto rotation before ios6

    // Override to allow orientations other than the default portrait orientation.

    • (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return UIInterfaceOrientationIsLandscape( interfaceOrientation ); }

But this method is deprecated since iOS 6. Instead, we need to add 2 methods in RootViewController.mm

// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead of shouldAutorotateToInterfaceOrientation
- (NSUInteger) supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}

- (BOOL) shouldAutorotate {
    return YES;
}

Comments

  • Wriggler

    Thanks for the heads up Walzer.

    2012-09-24 08:42
  • johngg123

    Does this also address the Game Center authentication crash in iOS6 ? I believe a portrait view MUST be supported for Game Center to not crash.

    See here:
    https://devforums.apple.com/message/731764\#731764
    http://www.cocos2d-iphone.org/forum/topic/36639

    2012-09-25 21:07
  • festival
    • (NSUInteger) supportedInterfaceOrientations will only be called when the App has a deployment version of iOS6 !

    Even when running on iOS6 device or compiled with iOS6 SDK (of course that must be done anyway).

    Setting a deployment target of i.e. iOS4.3 will use the deprecated method.

    2012-10-03 17:33
  • bulgasal

    My Game is compiled with ios 5.
    Then I don’t need those two hot fixes…?

    2012-10-03 18:20
  • nikhil

    [window setRootViewController:viewController]; is backward completable up to ios 4.0 :)

    2012-10-06 06:06
  • Jangli

    Does this also address the Game Center authentication crash in iOS6 ? I believe a portrait view MUST be supported for Game Center to not crash
    [http://www.whatmobile.alrazaak.com]

    2014-10-15 07:03

Copyright © 2010 - 2014 Cocos2d-x.org Support Email: support@cocos.org