[英]How to allow only these orientations in my Cordova/Phonegap app?
I created an iOS (iPhone) App using Cordova and want to only allow the following orientations: 我使用Cordova创建了一个iOS(iPhone)应用程序,并且只想允许以下方向:
This also means that "upside down" should not be allowed : 这也意味着不应该允许 “倒置”:
I know that I can set this in Xcode but when ever I start a new Cordova build this setting gets overwritten. 我知道我可以在Xcode中设置它,但是当我开始新的Cordova构建时,此设置会被覆盖。
So I checked Cordova docs and found this: http://cordova.apache.org/docs/en/5.1.1/config_ref_index.md.html#The%20config.xml%20File 所以我检查了Cordova文档,发现了这个: http : //cordova.apache.org/docs/en/5.1.1/config_ref_index.md.html#The%20config.xml%20File
It says that I can set orientation in config.xml like this: 它说我可以在config.xml中设置方向如下:
<preference name="Orientation" value="landscape" />
But I don't see how I can set a more fine granular setting as I described above. 但我没有看到如何设置更精细的粒度设置,如上所述。 How can this be done?
如何才能做到这一点?
Note: I am on Cordova 5.1.1 注意:我在Cordova 5.1.1上
You can use an after_prepare hook which will apply the settings after the cordova prepare
and therefore avoid them getting overwritten on each cordova build
. 您可以使用after_prepare挂钩,它将在
cordova prepare
之后应用设置,从而避免在每个cordova build
覆盖它们。 Place the following code in <your_project>/hooks/after_prepare/some_file.js
: 将以下代码放在
<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;
}
Note: you'll need to install the plist and xml2js node modules if you don't already have them. 注意:如果您还没有plist和xml2js节点模块,则需要安装它们。
You can use config.xml' 你可以使用config.xml'
<platform name="ios">
<preference name="Orientation" value="all" />
</platform>
along with shouldRotateToOrientation(degrees)
callback as stated in the docs like this: 以及如下文档中所述的
shouldRotateToOrientation(degrees)
回调:
onDeviceReady: function() {
app.receivedEvent('deviceready');
window.shouldRotateToOrientation = function(degrees) {
return degrees !== 180;
};
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.