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

Tim*_*imo 5 xcode ios cordova

我使用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" />
Run Code Online (Sandbox Code Playgroud)

但我没有看到如何设置更精细的粒度设置,如上所述.如何才能做到这一点?


注意:我在Cordova 5.1.1上

Dav*_*den 6

你可以使用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;
}
Run Code Online (Sandbox Code Playgroud)

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


das*_*rge 5

您可以使用config.xml'

<platform name="ios">
    <preference name="Orientation" value="all" />
</platform>
Run Code Online (Sandbox Code Playgroud)

以及像这样shouldRotateToOrientation(degrees)文档中所述的回调:

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