编辑:
根据@Abhinav Gujjar,导致cordova prepare 覆盖对.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 节点模块,则需要安装它们。