我使用Cordova创建了一个iOS(iPhone)应用程序,并且只想允许以下方向:
这也意味着不应该允许 "倒置" :
我知道我可以在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" />
Run Code Online (Sandbox Code Playgroud)
但我没有看到如何设置更精细的粒度设置,如上所述.如何才能做到这一点?
注意:我在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;
}
Run Code Online (Sandbox Code Playgroud)
注意:如果您还没有plist和xml2js节点模块,则需要安装它们.
您可以使用config.xml'
<platform name="ios">
<preference name="Orientation" value="all" />
</platform>
Run Code Online (Sandbox Code Playgroud)
以及像这样shouldRotateToOrientation(degrees)的文档中所述的回调:
onDeviceReady: function() {
app.receivedEvent('deviceready');
window.shouldRotateToOrientation = function(degrees) {
return degrees !== 180;
};
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2650 次 |
| 最近记录: |