Cordova 5生成命令正在删除iOS设备方向设置
对于Cordova 5.1.1,在执行“Cordova build ios”时,将删除先前在XCode项目中选择的所有设备方向设置,而不选中方向设置复选框 虽然“方向”配置首选项可能提供强制方向的方法,但我需要能够为iPad和iPhone设置不同的方向首选项 所有以前的Cordova版本(低于5)都遵守这些设置。有什么想法吗 使用XCode 6.3.2。编辑: 根据导致Cordova 5生成命令正在删除iOS设备方向设置,ios,iphone,xcode,cordova,cordova-5.0.0,Ios,Iphone,Xcode,Cordova,Cordova 5.0.0,对于Cordova 5.1.1,在执行“Cordova build ios”时,将删除先前在XCode项目中选择的所有设备方向设置,而不选中方向设置复选框 虽然“方向”配置首选项可能提供强制方向的方法,但我需要能够为iPad和iPhone设置不同的方向首选项 所有以前的Cordova版本(低于5)都遵守这些设置。有什么想法吗 使用XCode 6.3.2。编辑: 根据导致cordova prepare覆盖手动更改.plist中方向设置的问题,已修复。然而,恐怕在config.xml中仍然无法为iP
cordova prepare
覆盖手动更改.plist中方向设置的问题,已修复。然而,恐怕在config.xml中仍然无法为iPad和iPhone设置不同的方向偏好,因此下面的答案是正确的
更新:
我创建了这个插件,它包装了下面的钩子,意味着特定于平台的自定义配置块(比如这些方向设置)可以在config.xml中定义。因此,您可以使用插件,而无需手动创建下面的钩子
这是在Cordova 5.0.0 CLI-中引入的 与此同时,我一直在使用after_prepare挂钩作为解决方法。只需将以下内容放入
/hooks/after\u prepare/some\u 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节点模块,则需要安装它们。如果需要,可以在JS端以编程方式安装它们
对于iOS,可以通过定义
窗口上的javascript回调
在config.xml
文件中设置允许的方向
<platform name="ios">
<preference name="Orientation" value="all" />
</platform>
此问题已修复,现在可以在config.xml中将方向指定为“all”
<platform name="ios">
<preference name="Orientation" value="all" />
</platform>
此解决方案非常有效。起初我在没有sudo的情况下安装了plist和xml2js,我遇到了一些问题。一旦我卸载了这两个软件并用sudo重新安装,所有的软件都工作得很好。再次感谢。你把这些代码扔到哪里去了?就在AngularJS应用程序的index.html文件中?非常感谢您的关注。我玩得很开心。我在我的应用程序中升级了Cordova的iOS插件版本,它打破了方向旋转-至少在这之前对我有效-一件事,也许你可以把你的ifs放在外面,即根据设备设置回调,这样它只进行一次匹配-非常感谢顺便说一句,令人沮丧的是,导致方向偏好被覆盖的错误可能已经修复,但最初的问题是“我需要能够为iPad和iPhone设置不同的方向偏好”而且,如果使用config.xmlYou's right中的设置,这仍然是不可能的-OP确实需要不同的设备类型规格。我来这里是因为它把我锁在画像上了。我想我很高兴这个问题得到了解决。不过,很高兴知道这个问题得到了解决-谢谢你的更新:-)不幸的是,这个问题现在在cordova 5.4中被打破了:
onDeviceReady: function() {
app.receivedEvent('deviceready');
window.shouldRotateToOrientation = function(degrees) {
//if device an iPad ?
if ( navigator.userAgent.match(/iPad/i) ) {
return true;
}
//else if device an iPhone ?
else if (navigator.userAgent.match(/iPhone/i)) {
if (degrees == 0) { //orientation is portrait
return true;
}
return false; //refuse all other orientations for iPhone
}
return true;
};
}
<platform name="ios">
<preference name="Orientation" value="all" />
</platform>