簡體   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