繁体   English   中英

如何在我的Cordova / Phonegap应用程序中只允许这些方向?

[英]How to allow only these orientations in my Cordova/Phonegap app?

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

  • 肖像
  • 风景左
  • 景观正确

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

XCode截图设备方向

我知道我可以在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上

您可以使用after_prepare挂钩,它将在cordova prepare之后应用设置,从而避免在每个cordova build覆盖它们。 将以下代码放在<your_project>/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节点模块,则需要安装它们。

你可以使用config.xml'

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

以及如下文档中所述的shouldRotateToOrientation(degrees)回调:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM