Skip navigation
Currently Being Moderated

What is the replacement method for preventDefault() on iOS?

Sep 27, 2012 5:06 AM

Tags: #ios #preventdefault()

Using preventDefault() in response to orientationChanging is a well-documented technique for preventing rotation on iOS. What is the recommended replacement method for this call when manually managing device rotation events?


From the release notes: preventDefault() is not honored for the ORIENTATION_CHANGING event on iOS. This is a behavior change from AIR 3.4 and will remain so. (3324338)

  • Currently Being Moderated
    Sep 28, 2012 5:42 AM   in reply to gtr

    What seems a long time ago around Air 3.0 or lower, we decided to turn off orientation handling and roll our own method.  We simply listen to the accelerometer for changes in the x/y/z axis and react accordingly with a listener at the Application level.  We have a flag that individual views can use to enable/disable the orientation handling.  This may seem a little overkill, but once you have it in your app you shouldn't have to worry about inconstencies in the way orientation works in Air across versions.

    Mark as:
  • Currently Being Moderated
    Sep 28, 2012 9:32 AM   in reply to gtr

    Auto-orientation has drastically changed in iOS 6. Some of the auto-orientation callbacks have been completely deprecated in iOS 6. This change affects the screen orientation API's in AIR too and support for the new callbacks have been added in AIR 3.5 beta release. The deprecated callbacks informed the application about the new orientation it was being rotated to. Hence, application could decide whether it wanted to rotate to the new orientation or not. However, the new callbacks do not give us such information. The application is only queried about what orientations are currently supported. If this value returned by the new callbacks matches the new orientation stage is being rotated to, the stage automatically rotates. Otherwise it does not. As a result of this limitation of the new callbacks, support from preventDefault() was deprecated on iOS in AIR 3.5(built with iOS 6 SDK). The application will still receive the ORIENTATION_CHANGING event. But calling preventDefault() inside the ORIENTATION_CHANGING event handler won't prevent the stage from being rotated to the new orientation. It is important to note that apps packaged with iOS 6 and running on iOS 5.1 devices or lower will also see this behavior change of not able to control auto orientation by using preventDefault(). This was done to make this change consistent across iOS devices.

    Mark as:
  • Currently Being Moderated
    Oct 15, 2012 8:07 AM   in reply to Varun Bhatia

    Hi Varun, you also wrote on Air-o-dynamics:


    It is suggested to switch Stage.autoOrients to false as and when auto orientation is to be prevented.


    It seems that when I change autoOrients = false in my stageOrientationEvent listener, the listener then stops reporting any further ORIENTATION_CHANGING events. (My app is in landscape mode which needs to flip upside down on an iPad, but be prevented from entering portrait mode.)


    Can you or anyone else elaborate on controlling orientation without using preventDefault()? Thanks!

    Mark as:
  • Currently Being Moderated
    Oct 15, 2012 10:13 AM   in reply to daotting

    I don't have an iOS 6 device to test, but when publishing with AIR 3.4 or 3.5 this is working on an iPad 2 running iOS 5.1:


    In the -app.xml file (note that you have to set autoOrients to 'true'):







    Include this line of ActionScript:




    Result: App remains in landscape mode and rotates to change orientation only when the iPad is flipped upside down.


    Edit: On the What's New page in the beta docs ( 3/whatsnew.html) there are no methods or properties listed for handling orientation changes in the 'New classes and class elements added for Adobe AIR 3.5' table.

    Mark as:
  • Currently Being Moderated
    Oct 15, 2012 9:54 AM   in reply to daotting

    By setting autoOrients to false, I meant you are sure that you are turning off auto rotation on the current device. You wont receive orientation_changing events on switching off autoOrients. For an app that is supported in landscape and UpsideDown but not in Portrait, you dont seem to have any choice. You need to carefully choose the aspect ratio of the app. Kindly note that this restriction is introduced by iOS 6. The new callbacks dont allow us to prevent autorotation.

    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points