Cordova 5 build command is deleting iOS device orientation settings

时间:2015-06-15 15:12:52

标签: ios iphone xcode cordova cordova-5.0.0

With Cordova 5.1.1, when executing "cordova build ios", any device orientation settings previously selected in the XCode project are deleted leaving the orientation settings checkboxes unselected.

While the "orientation" config preference may provide a means of forcing orientation, I need to be able to set different orientation preferences for the iPad and iPhone.

All previous Cordova versions (below 5) respected those settings. Any ideas?

Using XCode 6.3.2.

3 个答案:

答案 0 :(得分:4)

修改

根据@Abhinav Gujjar,导致cordova prepare覆盖对.p列中的方向设置所做的手动更改的问题已修复。但是,AFAIK还没有办法在config.xml中为iPad和iPhone设置不同的方向偏好,所以下面的答案就是。

<强>更新

我创建了插件cordova-custom-config,它包含了下面的挂钩,意味着可以在config.xml中定义特定于平台的自定义配置块(例如这些方向设置)。所以你可以使用插件而不需要手动创建下面的钩子。

这是在Cordova 5.0.0 CLI中引入的 - see here

与此同时,我一直在使用after_prepare挂钩作为解决方法。只需将以下内容放在<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',
        'UIInterfaceOrientationPortraitUpsideDown'
    ];
    var iPadOrientations = [
            'UIInterfaceOrientationLandscapeLeft',
            'UIInterfaceOrientationLandscapeRight',
            'UIInterfaceOrientationPortrait',
            'UIInterfaceOrientationPortraitUpsideDown'
    ];
    plist["UISupportedInterfaceOrientations"] = iPhoneOrientations;
    plist["UISupportedInterfaceOrientations~ipad"] = iPadOrientations;
}

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

答案 1 :(得分:3)

如果你愿意,你可以在JS方面以编程方式进行。

  

对于iOS,可以通过定义a来以编程方式控制方向   窗口上的javascript回调

/** 
// @param {Number} degree - UIInterfaceOrientationPortrait: 0,
// UIInterfaceOrientationLandscapeRight: 90, 
// UIInterfaceOrientationLandscapeLeft: -90,     
// UIInterfaceOrientationPortraitUpsideDown: 180
* @returns {Boolean} Indicating if rotation should be allowed.
*/
function shouldRotateToOrientation(degrees) {
     return true;
}

config.xml文件中设置允许的方向

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

并添加shouldRotateToOrientation(degrees)

onDeviceReady: function() {
    app.receivedEvent('deviceready');
    window.shouldRotateToOrientation = function(degrees) {
        //if device an iPad ?
        if ( navigator.userAgent.match(/iPad/i) ) {
            return true;
        } 
        //else if device an iPhone ?
        else if (navigator.userAgent.match(/iPhone/i)) {
            if (degrees == 0) { //orientation is portrait
              return true;
            }
            return false; //refuse all other orientations for iPhone
        }
        return true;
    };
}

答案 2 :(得分:0)

此问题已修复,您现在可以在config.xml中将方向指定为“all”

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

Docs