簡體   English   中英

Cordova iOS橫向定位

[英]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

  1. 將以下內容添加到config.xml文件中:

     <platform name="ios"> <hook type="after_prepare" src="iosAfterPrepare.js" /> </platform> 
  2. 在頂級目錄中創建名為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. 你可能需要調用npm install --save plist來獲取你機器上的plist模塊(它會抱怨它找不到plist)。

  4. 呼叫:

     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插件。

來自corvoda-plugin-whitelist描述:

  • gap:僅在iOS上使用(使用UIWebView時),並且需要JS->本地通信

測試了cordova 8.1.2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM