【问题标题】:How to allow only these orientations in my Cordova/Phonegap app?如何在我的 Cordova/Phonegap 应用程序中只允许这些方向?
【发布时间】:2015-08-05 11:08:14
【问题描述】:

我使用 Cordova 创建了一个 iOS (iPhone) 应用程序并希望只允许以下方向:

  • 肖像
  • 横向左侧
  • 横向右侧

这也意味着“倒置”应该不允许

我知道我可以在 Xcode 中设置它,但是当我开始一个新的 Cordova 构建时,这个设置会被覆盖。

所以我检查了 Cordova 文档并发现了这个:http://cordova.apache.org/docs/en/5.1.1/config_ref_index.md.html#The%20config.xml%20File

它说我可以像这样在 config.xml 中设置方向:

<preference name="Orientation" value="landscape" />

但我不知道如何设置更精细的设置,如上所述。如何做到这一点?


注意:我使用的是 Cordova 5.1.1

【问题讨论】:

  • 使用值“默认”应该在 iPhone 上以这种方式工作,但我认为有一个错误。 cordova 3.9.0 昨天刚刚发布,试试那个版本
  • @jcesarmobile 当前 Cordova 版本为 5.1.1。同样默认将允许我不想要的“颠倒”。
  • 5.1.1 是最新的cordova CLI 版本,它带来了cordova iOS 3.8.0,但是您可以将您的iOS 项目更新到cordova iOS 3.9.0 已经

标签: ios xcode cordova


【解决方案1】:

你可以使用config.xml'

<platform name="ios">
    <preference name="Orientation" value="all" />
</platform>

连同shouldRotateToOrientation(degrees) 回调,如docs 中所述,如下所示:

onDeviceReady: function() {
    app.receivedEvent('deviceready');
    window.shouldRotateToOrientation = function(degrees) {
        return degrees !== 180;
    };
},

【讨论】:

  • 文档状态:“对于 iOS,可以通过在窗口上定义 javascript 回调以编程方式控制方向”。那么,shouldRotateToOrientation(degrees) 应该如何与 window 一起使用呢?
  • 使用用法更新了答案
  • 超级简单的解决方案!完美运行!非常感谢:-)
  • 不错的答案,但是有什么方法可以为 Iphone 和 iPad 设置不同的屏幕方向?我只需要 iphone 的 potrait 和 ipad 的landscape-potrait 。直到现在我只用 xcode 修复。有没有办法只在 config.xml 中设置?
【解决方案2】:

您可以使用 after_prepare 挂钩,它将在 cordova prepare 之后应用设置,从而避免它们在每个 cordova build 上被覆盖。将以下代码放入&lt;your_project&gt;/hooks/after_prepare/some_file.js

#!/usr/bin/env node

// Set support for all orienations in iOS .plist - workaround for this cordova bug: https://issues.apache.org/jira/browse/CB-8953
var platforms = process.env.CORDOVA_PLATFORMS.split(',');
platforms.forEach(function(p) {
    if (p == "ios") {
        var fs = require('fs'),
            plist = require('plist'),
            xmlParser = new require('xml2js').Parser(),
            plistPath = '',
            configPath = 'config.xml';
        // Construct plist path.
        if (fs.existsSync(configPath)) {
            var configContent = fs.readFileSync(configPath);
            // Callback is synchronous.
            xmlParser.parseString(configContent, function (err, result) {
                var name = result.widget.name;
                plistPath = 'platforms/ios/' + name + '/' + name + '-Info.plist';
            });
        }
        // Change plist and write.
        if (fs.existsSync(plistPath)) {
            var pl = plist.parseFileSync(plistPath);
            configure(pl);
            fs.writeFileSync(plistPath, plist.build(pl).toString());
        }
        process.exit();
    }
});
function configure(plist) {
    var iPhoneOrientations = [
        'UIInterfaceOrientationLandscapeLeft',
        'UIInterfaceOrientationLandscapeRight',
        'UIInterfaceOrientationPortrait'
    ];
    var iPadOrientations = [
            'UIInterfaceOrientationLandscapeLeft',
            'UIInterfaceOrientationLandscapeRight',
            'UIInterfaceOrientationPortrait'
    ];
    plist["UISupportedInterfaceOrientations"] = iPhoneOrientations;
    plist["UISupportedInterfaceOrientations~ipad"] = iPadOrientations;
}

注意:如果您还没有 plistxml2js 节点模块,则需要安装它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多