Cordova iOS横屏方向

3
我的Cordova应用在iPhone上运行时从未旋转到横屏模式。
我尝试了许多解决方案,如将这些行放入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>

我不知道接下来该做什么。谢谢。

1
如果您删除所有这些内容会发生什么?Cordova默认应该允许横向和纵向方向。如果您想要明确指定,请在config.xml中添加以下内容:<preference name="Orientation" value="default" /> - Jordan Burnett
只需在Xcode中进行设置即可。 - Joerg
5个回答

2

1

是的,这是cordova cli创建Xcode项目的一个缺点--它没有添加那些方向标签。

一段时间以前,我已经将以下内容添加到我的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文件中),而不适用于普通构建 - 因此它不会起作用。

所以...你最好的选择是:

  • 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:

    <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.


我尝试按照你告诉我的做,但是什么都不起作用。我还尝试将设置重置为默认值,但没有改变。无论是在模拟器上还是在iPhone设备上都不起作用。我注意到在构建时生成的.plist文件不包含方向设置,即使我尝试在/res/native/ios中覆盖此文件。还有其他想法吗?谢谢 - Rotan
我刚刚检查了第二个链接,看起来它是针对插件的,所以它对你没有帮助。我已经更新了我的答案——看起来你有两个选择:要么创建一个脚本将块添加到.plist文件中,要么手动从Xcode内部更改配置(正如@Joerg所提到的)。 - eb1
谢谢你的回复。但是我忘了提到我不使用Mac OS,我使用Cordova在Visual Studio上运行Windows。所以我无法找到.xcodeproj文件。 - Rotan
好的,这将进一步限制您的选择。.xcodeproj文件应该显示在您的platforms/ios目录中作为一个目录(在OS X中也是如此)。但是,如果您使用的是Windows,则无法在Xcode内手动进行方向更改。您唯一的选择是将我上面发布的代码块添加到您的.plist文件中。将该代码块添加到文件中的最后一个</dict></plist>之前。 - eb1
感谢您的帮助。无论如何,我尝试手动编辑在我的Cordova应用程序目录中找到的所有.plist文件。它们的内容在运行时完全被擦除,并且不考虑我的编辑。我想知道为什么会出现这种行为,因为当我针对Windows Phone时,我只是复制了基本的appxmanifest文件并设置了自己的属性,它可以正常工作。所以,明确一下,您的解决方案是在platform/ios中创建一个脚本,在运行时覆盖.plist内容吗? - Rotan

0

我看到你已经找到了一个解决方案。这里有另一个使用构建钩子和npm.js来完成工作的方法;好处是,如果你将来在Linux或OSX上进行开发,这应该可以跨平台使用。关于构建钩子的更多信息,请参见:http://cordova.apache.org/docs/en/latest/guide/appdev/hooks/index.html

将以下内容添加到你的 `config.xml` 文件中:
``` ```
在顶层目录中创建一个名为 `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 文件中看到这些行。

0

在 Cordova v 7.0.1 中对我起作用。

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

位于 config.xml 文件中。


0

对于iOS,您需要在index.html文件的Content Security Policy中添加“gap:// *”属性(需要两个斜杠)。 例如:

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

否则,只有当用户与操作系统交互(按前面的按钮、向下拖动显示通知中心或向上拖动进入设备设置)时,手机才会改变方向。

如果没有设置gap://*值,ondeready事件也不会触发。

您还需要添加cordova-plugin-whitelist插件。

corvoda-plugin-whitelist的描述中:

  • 仅在使用UIWebView时,iOS上需要gap:并且需要JS->本机通信

在cordova 8.1.2上进行了测试


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接