简体   繁体   English

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

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

I created an iOS (iPhone) App using Cordova and want to only allow the following orientations: 我使用Cordova创建了一个iOS(iPhone)应用程序,并且只想允许以下方向:

  • Portrait 肖像
  • Landscape Left 风景左
  • Landscape Right 景观正确

This also means that "upside down" should not be allowed : 这也意味着不应该允许 “倒置”:

XCode截图设备方向

I know that I can set this in Xcode but when ever I start a new Cordova build this setting gets overwritten. 我知道我可以在Xcode中设置它,但是当我开始新的Cordova构建时,此设置会被覆盖。

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

It says that I can set orientation in config.xml like this: 它说我可以在config.xml中设置方向如下:

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

But I don't see how I can set a more fine granular setting as I described above. 但我没有看到如何设置更精细的粒度设置,如上所述。 How can this be done? 如何才能做到这一点?


Note: I am on Cordova 5.1.1 注意:我在Cordova 5.1.1上

You can use an after_prepare hook which will apply the settings after the cordova prepare and therefore avoid them getting overwritten on each cordova build . 您可以使用after_prepare挂钩,它将在cordova prepare之后应用设置,从而避免在每个cordova build覆盖它们。 Place the following code in <your_project>/hooks/after_prepare/some_file.js : 将以下代码放在<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;
}

Note: you'll need to install the plist and xml2js node modules if you don't already have them. 注意:如果您还没有plistxml2js节点模块,则需要安装它们。

You can use config.xml' 你可以使用config.xml'

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

along with shouldRotateToOrientation(degrees) callback as stated in the docs like this: 以及如下文档中所述的shouldRotateToOrientation(degrees)回调:

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

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

相关问题 Cordova-iOS-不允许所有方向 - Cordova - iOS - Can't allow all orientations 如何允许用户从应用程序内以特定方向使用应用程序? - How to allow users to use application in particular orientations from within app? 如何缩放Cordova / PhoneGap应用程序? 我应该使用“ cordova-anyscreen”,还是太笨拙? - How can I scale my Cordova/PhoneGap app? Should I use “cordova-anyscreen”, or is it too hacky? 仅允许一个视图控制器的所有方向 - Allow all orientations for only one view controller 仅对一个UIViewController允许多个方向 - Allow multiple orientations for only one UIViewController 如何从其他应用程序将图像共享到我的Cordova / PhoneGap应用程序? - How can I share an Image to my Cordova/PhoneGap App from another App? 如何在应用程序浏览器中触发Cordova / PhoneGap - How to trigger Cordova/PhoneGap In App Browser 如何与我的Cordova / PhoneGap应用(ios)共享Safari URL? - How can I share Safari URL to my Cordova/PhoneGap App (ios)? 如何允许仅通过移动应用程序请求我的API? - How allow to request my API only by my mobile app? 如何只允许我自己的应用访问我的API - How to only allow my own app to access my API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM