Cordova在iPad上锁定方向失败

8

我正在使用cordova 3.5.0-0.2.6(最后一个稳定版本)。

我在iPad设备上锁定方向时遇到了问题。

在iPhone上它可以正常工作,但在iPad上方向没有被锁定。

我想锁定整个应用程序而不仅仅是页面。

这是我的当前config.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<widget id="com.domain"
        version="version"
        xmlns="http://www.w3.org/ns/widgets">
    <name>xxx</name>

    <description>Lorem ipsum</description>

    <access origin="*"/>

    <author email="x@x" href="https://x.com">x</author>

    <content src="index.html?platform=cordova"/>

    <feature ...></feature>

    <preference name="permissions" value="none"/>
    <preference name="orientation" value="portrait"/>
    <preference name="show-splash-screen-spinner" value="true"/>
    <preference name="auto-hide-splash-screen" value="true"/>
    <preference name="prerendered-icon" value="true"/>
    <preference name="disallowoverscroll" value="true"/>
    <preference name="webviewbounce" value="false"/>

    <preference name="StatusBarOverlaysWebView" value="false"/>
    <preference name="StatusBarBackgroundColor" value="#000000"/>
</widget>

生成的 plist 文件如下所示:
<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations¨ipad</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationPortraitUpsideDown</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

2
这是由于 Cordova 中的一个错误 - https://issues.apache.org/jira/browse/CB-6026 还有一个更大的与方向相关的错误尚未修复 - https://issues.apache.org/jira/browse/CB-6462 - Mauro
2个回答

2
有一个不太正式的解决方案,但处理这个问题的一种方法是通过Cordova钩子。例如,将其放置在hooks/before_compile目录中:
var fs = require('fs');
var plist = './platforms/ios/YourProjectName/YourProjectName-Info.plist';
fs.exists(plist, function (exists) {
    if (exists) {
        var p = fs.readFileSync(plist, 'utf8');
        p = p.replace(
            /<key>(UISupportedInterfaceOrientations(\~ipad)*)<\/key>[\r\n ]*<array>[\s\S]*?(?=<\/array>)/ig,
            "<key>$1</key>\n\t<array>\n\t\t<string>UIInterfaceOrientationLandscapeLeft</string>\n\t\t<string>UIInterfaceOrientationLandscapeRight</string>\n\t"
        );
        fs.writeFileSync(plist, p, "utf8");
    }
});

当您构建iOS应用(cordova build ios)时,它应自动修改plist文件。

非常好的想法,考虑到它对我仍然不起作用。我不得不使用: p = p.replace( "<dict>", "<dict>\n\t\t<key>UISupportedInterfaceOrientationsiphone</key>\n\t\t<array>\n\t\t<string>UIInterfaceOrientationPortrait</string>\n\t\t</array>\n\t\t<key>UISupportedInterfaceOrientationsipad</key>\n\t\t<array>\n\t\t<string>UIInterfaceOrientationPortrait</string>\n\t\t<string>UIInterfaceOrientationLandscapeLeft</string>\n\t\t<string>UIInterfaceOrientationLandscapeRight</string>\n\t\t</array>\n\t"
);
- Ben Taliadoros

2
我尝试了许多解决方法来解决这个错误,但大部分都失败了。幸运的是,我找到了一个 Cordova 插件,可以通过 JavaScript 成功锁定屏幕方向,在 iPad 上也可以使用。
插件链接:https://github.com/yoik/cordova-yoik-screenorientation 操作步骤如下:
1. 添加插件:cordova plugin add net.yoik.cordova.plugins.screenorientation 2. 在 JavaScript 中使用 screen.lockOrientation('portrait-primary') 锁定屏幕方向。确保在文档的 deviceready 事件触发后调用此函数。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接