使用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',
'UIInterfaceOrientationLandscapeRight',
'UIInterfaceOrientationPortrait',
'UIInterfaceOrientationPortraitUpsideDown'
];
plist["UISupportedInterfaceOrientations"] = iPhoneOrientations;
plist["UISupportedInterfaceOrientations~ipad"] = iPadOrientations;
}
注意:如果您还没有plist和xml2js节点模块,则需要安装它们.
