6 Replies Latest reply: Oct 15, 2012 9:54 AM by Varun Bhatia RSS

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

    gtr Community Member

      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)

        • 1. Re: What is the replacement method for preventDefault() on iOS?
          ChristopherTotty Community Member

          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.

          • 2. Re: What is the replacement method for preventDefault() on iOS?
            Varun Bhatia Adobe Employee

            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.

            • 3. Re: What is the replacement method for preventDefault() on iOS?
              gtr Community Member

              Thanks for the information, Varun. Looking forward to hearing about the new methods in AIR 3.5.

               

              @ChristopherTotty  That's an interesting approach! A little over my head at the moment, but good to know of alternate methods. Thanks!

              • 4. Re: What is the replacement method for preventDefault() on iOS?
                daotting Community Member

                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!

                • 5. Re: What is the replacement method for preventDefault() on iOS?
                  random(1) Community Member

                  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'):

                   

                  <initialWindow>

                      <aspectRatio>landscape</aspectRatio>

                      <autoOrients>true</autoOrients>

                  </initialWindow>

                   

                  Include this line of ActionScript:

                   

                  stage.setAspectRatio(StageAspectRatio.LANDSCAPE);

                   

                  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 (http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/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.

                  • 6. Re: What is the replacement method for preventDefault() on iOS?
                    Varun Bhatia Adobe Employee

                    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.