If you are making AIR native extensions which use any form of native window, you may notice one of two things:

  • The native window seems to have a mind of its own when it comes to orientation. In other words, if you want to keep your app Landscape only, the minute a native window shows up, the whole app may change its orientation to Portrait.
  • The native window seems to have acquired a mind of its own. You had the native code all nicely set up, so that native and AIR views play nicely together and agree to orientation, but after moving to iOS 6, anarchy rules again.

How to ensure native windows don’t mess up your app’s orientation on iOS 6

Here is the recipe: override supportedInterfaceOrientations in your window’s implementation in your XCode project and have it return UIInterfaceOrientationMaskAll:

[sourcecode language=”objc”]
– ( NSUInteger ) supportedInterfaceOrientations
return UIInterfaceOrientationMaskAll;

The equivalent on iOS 5 was shouldAutorotateToInterfaceOrientation, which was deprecated in the iOS 6 SDK:

[sourcecode language=”objc”]
– ( BOOL ) shouldAutorotateToInterfaceOrientation : ( UIInterfaceOrientation ) toInterfaceOrientation
return YES;

Wait, I don’t have access to the native window’s implementation!

There is a way out of this situation too: add a category to UIViewController and override supportedInterfaceOrientations in the category.

Categories in Objective-C are a way of adding or overriding methods in existing classes without subclassing them.

In this case UIViewController.

Make sure you import the class category in your code, in order for the overridden method to be called:

Why return UIInterfaceOrientationMaskAll if I want to restrict my app to Landscape only?

Kudos to you for asking this question! Like you, in DiaDraw we hate having to copy-paste recipes without knowing why they work.

An AIR Native Extension will typically present a view controller which is child of the root view controller, i.e. the controller of your AIR main window. When a root view needs to be rotated, it asks its children (subviews) which orientations they support. If the orientation that the view was headed for is not supported by a child, the rotation is overridden towards whatever orientation the child supports.

If child and root view controller don’t have any orientations in common they support, an exception is thrown and your app crashes.

Where can I see examples?

We have updated our Open-Source native extensions to play nicely with orientation on iOS 6. Two of these are a good example of the two cases in which child view controllers have to be set up to support all orientations:

I would like to add something…

Please do, join the conversation and share your experience with native extensions by leaving a comment below:

About the Author

Radoslava is co-founder of DiaDraw. Prefers to communicate with images. Verbal communication always caused trouble with her parents. Started speaking Basic early on, followed by four years of Delphi, six years of C++, four years of ActionScript, lately converses in Objective-C. Her mum and dad hope she'll start speaking human at some point.