![](/img/trans.png)
[英]Enforce right orientation landscape mode for Phonegap/Cordova iOS app
[英]Cordova iOS landscape orientation
在iPhone上运行时,我的Cordova应用程序永远不会旋转到横向模式。
我已经尝试了很多解决方案,因为将这些行放在config.xml文件中:
<preference name="ios-orientation-iphone" value="portrait and landscape" />
<preference name="ios-orientation-ipad" value="portrait and landscape" />
<preference name="Orientation" value="default" />
我还将以下行放在<platform name="ios">
块中:
<preference name="Orientation" value="all" />
然后我在index.js文件中执行了以下操作:
window.shouldRotateToOrientation = function (degrees) {
return true;
};
最后,我尝试在res / native / ios文件夹中创建一个自定义plist文件,因为我注意到生成的plist文件不包含这些行:
<key>UISupportedInterfaceOrientations</key>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
我不知道接下来该做什么。 谢谢
我终于做到了。 诀窍就是将自定义.plist文件放在res / native // - Info.plist中。
这个链接给了我解决方案: Apache Cordova工具(VS2015):为iOS的* info.plist添加自定义条目
是的,这是创建Xcode项目的cordova cli的缺点 - 它不会添加这些方向标记。
前段时间我将以下内容添加到我的config.xml中(我目前正在使用PhoneGap构建服务)。
<gap:config-file platform="ios" parent="UISupportedInterfaceOrientations" mode="replace">
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
</gap:config-file>
有关此博客文章的更多信息: http : //phonegap.com/blog/2014/01/30/customizing-your-android-manifest-and-ios-property-list-on-phonegap-build/ 。
更新 :我有<config-file>
元素的链接,但看起来该元素用于插件(在plugin.xml文件中),而不是正常构建 - 所以它不起作用。
那么......你最好的赌注是:
要以编程方式添加方向内容,请创建一个查找.plist文件的脚本,如果该块不存在,则将以下块添加到其中:
<key>UISupportedInterfaceOrientations</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationPortraitUpsideDown</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array>
要在通过cordova platform add ios
添加平台后添加它,打开.xcodeproj文件,转到项目节点/常规/部署信息,并检查iPhone和iPad的所有方向。
我看到你找到了解决方案。 这是另一个使用构建钩子和npm.js来完成工作的人; 如果您在Linux或OSX上进行开发,那么这应该是跨平台的。 以下是有关构建钩子的更多信息: http : //cordova.apache.org/docs/en/latest/guide/appdev/hooks/index.html
将以下内容添加到config.xml
文件中:
<platform name="ios"> <hook type="after_prepare" src="iosAfterPrepare.js" /> </platform>
在顶级目录中创建名为iosAfterPrepare.js
的文件,然后复制以下内容(将MyProject替换为项目名称):
// iosAfterPrepare.js // Node.js build script run after "cordova ios prepare" (part of cordova build). // This adds the various orientations to the plist file for your project. module.exports = function (context) { var fs = require('fs'), // nodejs.org/api/fs.html plist = require('plist'), // www.npmjs.com/package/plist // Here you will replace "MyProject" with the name of yours, // so that the .plist file can be found FILEPATH = 'platforms/ios/MyProject/MyProject-Info.plist', xml = fs.readFileSync(FILEPATH, 'utf8'), obj = plist.parse(xml); obj.UISupportedInterfaceOrientations = [ "UIInterfaceOrientationPortrait", "UIInterfaceOrientationPortraitUpsideDown", "UIInterfaceOrientationLandscapeLeft", "UIInterfaceOrientationLandscapeRight" ]; xml = plist.build(obj); fs.writeFileSync(FILEPATH, xml, { encoding: 'utf8' }); };
你可能需要调用npm install --save plist
来获取你机器上的plist模块(它会抱怨它找不到plist)。
呼叫:
cordova platform rm ios cordova platform add ios
此时您应该在.plist文件中看到这些行。
这在cordova v 7.0.1中对我有用
<preference name="Orientation" value="default" />
在config.xml
对于iOS,您需要在index.html文件中的“内容安全策略”中添加“gap:// *”属性(需要两个版本)。 例如:
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data:* gap://* tel:* 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'" />
只有当用户与操作系统交互时,才会改变方向(按下前面的按钮,通过向下拖动显示通知中心或向上拖动设备设置)。
ondeviceready事件既不会在没有间隙的情况下触发:// *值设置。
您还需要添加cordova-plugin-whitelist插件。
- gap:仅在iOS上使用(使用UIWebView时),并且需要JS->本地通信
测试了cordova 8.1.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.