加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cordova 5 build命令正在删除iOS设备方向设置

发布时间:2020-12-14 17:59:17 所属栏目:百科 来源:网络整理
导读:使用Cordova 5.1.1时,执行“cordova build ios”时,先前在XCode项目中选择的任何设备方向设置都将被删除,而未选中方向设置复选框. 虽然“方向”配置首选项可能提供强制定位的方法,但我需要能够为iPad和iPhone设置不同的方向首选项. 所有以前的Cordova版本(低
使用Cordova 5.1.1时,执行“cordova build ios”时,先前在XCode项目中选择的任何设备方向设置都将被删除,而未选中方向设置复选框.

虽然“方向”配置首选项可能提供强制定位的方法,但我需要能够为iPad和iPhone设置不同的方向首选项.

所有以前的Cordova版本(低于5)都尊重这些设置.有任何想法吗?

使用XCode 6.3.2.

解决方法

编辑:

根据@Abhinav Gujjar,修复了导致cordova准备覆盖对.plist中的方向设置所做的手动更改的问题.但是,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','UIInterfaceOrientationPortraitUpsideDown'
    ];
    plist["UISupportedInterfaceOrientations"] = iPhoneOrientations;
    plist["UISupportedInterfaceOrientations~ipad"] = iPadOrientations;
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读