简体   繁体   English

Cordova iOS横向定位

[英]Cordova iOS landscape orientation

My Cordova app never rotates to landscape mode when running on iPhone. 在iPhone上运行时,我的Cordova应用程序永远不会旋转到横向模式。

I've tried many solutions as putting these lines in the config.xml files: 我已经尝试了很多解决方案,因为将这些行放在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" />

I also put the following line inside the <platform name="ios"> block: 我还将以下行放在<platform name="ios">块中:

<preference name="Orientation" value="all" />

Then I did the following in my index.js file: 然后我在index.js文件中执行了以下操作:

        window.shouldRotateToOrientation = function (degrees) {
            return true;
        };

Finally, I tried to create a custom plist file in the res/native/ios folder cause I noticed than the generated plist file didn't contain these lines: 最后,我尝试在res / native / ios文件夹中创建一个自定义plist文件,因为我注意到生成的plist文件不包含这些行:

            <key>UISupportedInterfaceOrientations</key>
            <string>UIInterfaceOrientationLandscapeLeft</string>
            <string>UIInterfaceOrientationLandscapeRight</string>
            <string>UIInterfaceOrientationPortrait</string>
            <string>UIInterfaceOrientationPortraitUpsideDown</string>

I don't know what to do next. 我不知道接下来该做什么。 Thanks 谢谢

I finally did it. 我终于做到了。 The trick was just to put the custom .plist file in the res/native//-Info.plist. 诀窍就是将自定义.plist文件放在res / native // - Info.plist中。

This link gave me the solution: Tools for Apache Cordova (VS2015): Adding custom entries to *info.plist for iOS 这个链接给了我解决方案: Apache Cordova工具(VS2015):为iOS的* info.plist添加自定义条目

Yeah, this is a shortcoming of the cordova cli that creates the Xcode project -- it doesn't add those orientation tags. 是的,这是创建Xcode项目的cordova cli的缺点 - 它不会添加这些方向标记。

A while back I had added the following to my config.xml (I'm using the PhoneGap build service currently). 前段时间我将以下内容添加到我的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>

There's more info on this blog post: http://phonegap.com/blog/2014/01/30/customizing-your-android-manifest-and-ios-property-list-on-phonegap-build/ . 有关此博客文章的更多信息: http//phonegap.com/blog/2014/01/30/customizing-your-android-manifest-and-ios-property-list-on-phonegap-build/


Update : I had a link the <config-file> element, but it looks like that element is for plugins (in the plugin.xml file), not for the normal build -- so it won't work. 更新 :我有<config-file>元素的链接,但看起来该元素用于插件(在plugin.xml文件中),而不是正常构建 - 所以它不起作用。

So... your best bets are: 那么......你最好的赌注是:

  • To programmatically adding orientation stuff, create a script that finds your .plist file and adds the following block to it if the block isn't there: 要以编程方式添加方向内容,请创建一个查找.plist文件的脚本,如果该块不存在,则将以下块添加到其中:

     <key>UISupportedInterfaceOrientations</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationPortraitUpsideDown</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> 
  • To add it after the platform is added via cordova platform add ios , open the .xcodeproj file, go to the project node / General / Deployment info, and check all the orientations for both iPhone and iPad. 要在通过cordova platform add ios添加平台后添加它,打开.xcodeproj文件,转到项目节点/常规/部署信息,并检查iPhone和iPad的所有方向。

I see you've found a solution. 我看到你找到了解决方案。 Here's another one that uses build hooks and npm.js to do the work; 这是另一个使用构建钩子和npm.js来完成工作的人; the benefit is that this should work cross-platform, should you ever do development on linux or OSX. 如果您在Linux或OSX上进行开发,那么这应该是跨平台的。 Here's more info about build hooks: http://cordova.apache.org/docs/en/latest/guide/appdev/hooks/index.html 以下是有关构建钩子的更多信息: http//cordova.apache.org/docs/en/latest/guide/appdev/hooks/index.html

  1. Add the following to your config.xml file: 将以下内容添加到config.xml文件中:

     <platform name="ios"> <hook type="after_prepare" src="iosAfterPrepare.js" /> </platform> 
  2. Create a file named iosAfterPrepare.js in the top-level directory and copy in the following (replacing MyProject with the name of your project): 在顶级目录中创建名为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' }); }; 
  3. You might need to do call npm install --save plist to get the plist module on your machine (it'll complain that it can't find plist). 你可能需要调用npm install --save plist来获取你机器上的plist模块(它会抱怨它找不到plist)。

  4. Call: 呼叫:

     cordova platform rm ios cordova platform add ios 

You should see the lines in the .plist file at this point. 此时您应该在.plist文件中看到这些行。

This worked for me in cordova v 7.0.1 这在cordova v 7.0.1中对我有用

<preference name="Orientation" value="default" />

in config.xml config.xml

For iOS you need to add "gap://*" property into Content Security Policy in your index.html file (two slashed are required). 对于iOS,您需要在index.html文件中的“内容安全策略”中添加“gap:// *”属性(需要两个版本)。 For example: 例如:

 <meta http-equiv="Content-Security-Policy" content="default-src 'self' data:* gap://* tel:* 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'" />

Othewise phone changes orientation only if the user interacts with OS (pressing the front button, showing the notification center with drag down or go to device settings dragging up). 只有当用户与操作系统交互时,才会改变方向(按下前面的按钮,通过向下拖动显示通知中心或向上拖动设备设置)。

ondeviceready event will neither fire without gap://* value set. ondeviceready事件既不会在没有间隙的情况下触发:// *值设置。

You also need to add cordova-plugin-whitelist plugin. 您还需要添加cordova-plugin-whitelist插件。

from corvoda-plugin-whitelist description: 来自corvoda-plugin-whitelist描述:

  • gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication gap:仅在iOS上使用(使用UIWebView时),并且需要JS->本地通信

Tested on cordova 8.1.2 测试了cordova 8.1.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM