Cordova iOS横向定位
我的Cordova应用程序在iPhone上运行时从不旋转到横向模式 我在config.xml文件中尝试了许多解决方案:Cordova iOS横向定位,ios,cordova,Ios,Cordova,我的Cordova应用程序在iPhone上运行时从不旋转到横向模式 我在config.xml文件中尝试了许多解决方案: <preference name="ios-orientation-iphone" value="portrait and landscape" /> <preference name="ios-orientation-ipad" value="portrait and landscape" /> <preference name="O
<preference name="ios-orientation-iphone" value="portrait and landscape" />
<preference name="ios-orientation-ipad" value="portrait and landscape" />
<preference name="Orientation" value="default" />
最后,我尝试在res/native/ios文件夹中创建自定义plist文件,因为我注意到生成的plist文件不包含以下行:
<key>UISupportedInterfaceOrientations</key>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
UI支持接口方向
UIInterface方向和左视图
UIInterfaceOrientationAndscapeRight
UIInterfaceOrientationPortrait
UIInterfaceOrientation上下方向图
我不知道下一步该怎么办。谢谢是的,这是创建Xcode项目的cordova cli的一个缺点——它没有添加那些方向标记 不久前,我在config.xml中添加了以下内容(我目前正在使用PhoneGap构建服务)
.
更新:我在
元素上有一个链接,但该元素似乎是用于插件的(在plugin.xml文件中),而不是用于正常的构建--因此它无法工作
所以。。。您的最佳赌注是:
- 要以编程方式添加方向信息,请创建一个脚本来查找.plist文件,如果该块不存在,则向其中添加以下块:
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationPortraitUpsideDown</string>
<string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string>
</array>
UI支持接口方向
UIInterfaceOrientationPortrait
UIInterfaceOrientation上下方向图
UIInterface方向和左视图
UIInterfaceOrientationAndscapeRight
- 要在通过cordova platform add ios添加平台后添加它,请打开.xcodeproj文件,转到项目节点/General/Deployment info,并检查iPhone和iPad的所有方向
我终于做到了。诀窍就是将自定义.plist文件放在res/native/-Info.plist中
此链接为我提供了解决方案:我看到您找到了解决方案。下面是另一个使用构建钩子和npm.js来完成这项工作的工具;这样做的好处是,如果您曾经在linux或OSX上进行过开发,那么它应该可以跨平台工作。以下是有关生成挂钩的更多信息:
- 将以下内容添加到
config.xml
文件中:
<platform name="ios">
<hook type="after_prepare" src="iosAfterPrepare.js" />
</platform>
- 您可能需要执行call
npm install--save plist
以在您的计算机上获取plist模块(它会抱怨找不到plist)
- 电话:
cordova platform rm ios
cordova platform add ios
此时应该可以看到.plist文件中的行 这在cordova V7.0.1中对我有效
在iOS的config.xml
中,您需要在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插件白名单插件
从描述:
- gap:仅在iOS上(使用UIWebView时)需要,用于JS->native通信
在cordova 8.1.2上进行测试如果将所有这些都删除,会发生什么?默认情况下,Cordova应允许横向和纵向方向。只添加:
到您的config.xml中,如果您想明确说明,只需在Xcode中进行设置。我尝试执行您告诉我的所有操作,但没有任何效果。我还尝试将设置重置为默认值,但没有更改。它在emulator和iPhone设备上都不起作用。我注意到生成时生成的.plist文件不包含方向设置,即使我尝试在/res/native/ios中重写此文件。还有别的想法吗?ThanksI刚刚检查了第二个链接,看起来它是特定于插件的,所以对您没有帮助。我已经更新了我的答案——看起来您可以选择创建一个脚本,将块添加到.plist文件中,或者从Xcode中手动更改配置(如@Joerg所述)。感谢您的回复。但我忘了提到我不在Mac OS上工作。我在Visual Studio上使用Cordova在Windows上工作。所以我找不到.xcodeproj文件。好吧,这就更限制了你的选择。.xcodeproj文件应该作为一个目录显示在您的platforms/ios目录中(在OSX中也是如此)。但是如果您在Windows上,您将无法从Xcode中手动更改方向。您唯一的选择是将我上面发布的块添加到您的.plist文件中。在文件中最后一个
之前添加块。感谢您的帮助。无论如何,我试图手动编辑我在Cordova应用程序目录中找到的所有.plist文件。它们的内容在运行时被完全擦除,不考虑我的编辑。我想知道为什么会发生这种行为,因为当我以Windows Phone为目标时,我只是复制了基本的appxmanifest文件并设置了自己的属性,而且效果很好。所以,要清楚,您的解决方案是在平台/ios中创建一个脚本,以便在运行时覆盖.plist内容?
<platform name="ios">
<hook type="after_prepare" src="iosAfterPrepare.js" />
</platform>
// 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' });
};
cordova platform rm ios
cordova platform add ios
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data:* gap://* tel:* 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'" />