且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我的 Cordova/PhoneGap iOS 应用在设备旋转时不旋转?

更新时间:2022-12-27 11:00:28

PiTheNumber 的答案对于那些可以修改 Cordova 生成的本机代码的人来说看起来不错.

在 Cordova 上关注 这个 JIRA 问题,并按照在本博客中,您还可以使用 plist 值或定义一个 window.shouldRotateToOrientation 函数Javascript 代码,非常适合我.

window.shouldRotateToOrientation = function(degrees) {返回真;}

这将为当前页面启用设备方向(因此,对于您的整个应用程序,如果它是大多数 Cordova 应用程序的单页应用程序").请注意,您还可以根据以度为单位的旋转值来决定启用它,甚至为什么不启用它,仅在某些视图上启用它,或者让用户在您的 HTML 应用程序中选择...很好,不是吗.>

为了记录,我不需要做任何事情来获得 iOS 8 iPad 手柄旋转,而 iOS 6 和 iOS 7 iPhone 在当前的 Cordova 版本(4.2.0,cordova)中默认不会处理它ios 平台版本ios 3.7.0").那是因为可以在 Xcode 上为每种设备类型"(平板电脑/手机)授予不同的旋转设置.需要注意的是,Cordova 会先检查上面的 JS 函数是否存在,然后如果该函数不存在或不允许旋转,则使用 Xcode 旋转设置.

I am trying to make a landscape only app, but I am not able to produce any rotation at all.

There used to be a autorotate setting in PhoneGap.plist but in phonegap 1.8.0 I can find it. Does it still exists?

What else could be wrong that my application is not rotating?

UPDATE

I know have webpage containing only one word "test". I set target device to iPad only and enabled all four orientations. What could still be wrong?

Do need to have a special html document type? Do I need to include some cordova-1.8.0.js? I could not find one for iOS (!?!) so i tested it with the android version. I read the API is now the same so can I use the android .js file?

PiTheNumber's answer looks OK for those fine with modifying the Cordova-generated native code.

Following this JIRA issue on Cordova, and as neatly explained in this blog, you can also use plist values or define a window.shouldRotateToOrientation function in your Javascript code, which suits me very well.

window.shouldRotateToOrientation = function(degrees) {
 return true;
}

This would enable device orientation for the current page (so, for your whole app if it's a "one page app" as most Cordova apps are). Note you can also decide to enable it based on the rotation value in degrees, or even, why not, enable it only on certain views, or letting the user choose within your HTML app... Nice, isn't it.

For the record, I didn't need to do anything to get an iOS 8 iPad handle rotation, while both iOS 6 and iOS 7 iPhones wouldn't handle it by default in the current Cordova version (4.2.0, cordova ios platform version "ios 3.7.0"). That is because one can grant different rotation settings per "device type" (tablet / phone) on Xcode. The thing to note is that Cordova will first check the JS function above if it exists, and then if the function does not exist or it did not allow the rotation, the Xcode rotation setting will be used.