iOS自定义方向配置

时间:2018-01-08 22:17:31

标签: ios cordova

我正在寻找一种方法为Cordova中的iOS应用指定两种不同的方向。一套定位适用于iPad,另一套适用于iPhone。他们应该如下:iPhone - 只有肖像(常规和颠倒),iPad - 所有4个方向。

到目前为止,我已经使用了config.xml

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

[APP_NAME]-Info.plist文件中生成以下内容:

<key>UIInterfaceOrientation</key>
<array>
  <string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
  <string>UIInterfaceOrientationPortrait</string>
  <string>UIInterfaceOrientationPortraitUpsideDown</string>
  <string>UIInterfaceOrientationLandscapeLeft</string> <!-- remove this -->
  <string>UIInterfaceOrientationLandscapeRight</string> <!-- and remove this -->
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
  <string>UIInterfaceOrientationPortrait</string>
  <string>UIInterfaceOrientationPortraitUpsideDown</string>
  <string>UIInterfaceOrientationLandscapeLeft</string>
  <string>UIInterfaceOrientationLandscapeRight</string>
</array>

然后我删除上面评论的行,以便在构建时给出我想要的内容。当然,我希望在发布之前不需要注意的解决方案。

到目前为止,我已尝试深入研究Cordova文档,但唯一提到的类似内容是config-file条目的plugin.xml部分,但由于这不是插件 - 具体配置,我无法看到这是正确的(即plugin.xml属于哪个?)

如果可能的话,我更愿意通过配置来做到这一点。

修改

来自@ jcaron的评论我尝试将config-file元素直接添加到config.xml中,如下所示:

<platform name="ios">
  <config-file target="*-Info.plist" parent="UISupportedInterfaceOrientations">
    <array>
      <string>UIInterfaceOrientationPortrait</string>
      <string>UIInterfaceOrientationPortraitUpsideDown</string>
    </array>
  </config-file>
</platform>

以及

<config-file target="*-Info.plist" parent="UISupportedInterfaceOrientations">
  <array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationPortraitUpsideDown</string>
  </array>
</config-file>

这些都没有奏效。我也尝试改变路径,在上述两种情况下都预先APP_NAME/ios/APP_NAMEplatforms/ios/APP_NAME,但没有成功。

1 个答案:

答案 0 :(得分:1)

您可以使用cordova-custom-config插件在after_prepare挂钩上应用更改,以确保它们不会被Cordova默认值覆盖(如上面评论中指出的@jcesarmobile

将插件添加到项目中:

cordova plugin add cordova-custom-config

config.xml

中定义自定义配置文件块
<platform name="ios">
    <custom-config-file platform="ios" target="*-Info.plist" parent="UISupportedInterfaceOrientations" mode="replace">
       <array>
           <string>UIInterfaceOrientationPortrait</string>
           <string>UIInterfaceOrientationPortraitUpsideDown</string>
       </array>
    </custom-config-file>
    <custom-config-file platform="ios" target="*-Info.plist" parent="UISupportedInterfaceOrientations~ipad" mode="replace">
       <array>
            <string>UIInterfaceOrientationPortrait</string>
            <string>UIInterfaceOrientationPortraitUpsideDown</string>
            <string>UIInterfaceOrientationLandscapeLeft</string>
            <string>UIInterfaceOrientationLandscapeRight</string>
       </array>
    </custom-config-file>
</platform>