iPad上的Cordovalocking方向失败

我正在使用cordova 3.5.0-0.2.6(最后一个稳定版本)。 我在lockingiPad设备方向时遇到问题。 在iPhone上它工作正常,但在iPad上的方向不locking。

我想locking整个应用程序,而不仅仅是页面。

这是我目前的config.xml:

<?xml version="1.0" encoding="utf-8"?> <widget id="com.domain" version="version" xmlns="http://www.w3.org/ns/widgets"> <name>xxx</name> <description>Lorem ipsum</description> <access origin="*"/> <author email="x@x" href="https://x.com">x</author> <content src="index.html?platform=cordova"/> <feature ...></feature> <preference name="permissions" value="none"/> <preference name="orientation" value="portrait"/> <preference name="show-splash-screen-spinner" value="true"/> <preference name="auto-hide-splash-screen" value="true"/> <preference name="prerendered-icon" value="true"/> <preference name="disallowoverscroll" value="true"/> <preference name="webviewbounce" value="false"/> <preference name="StatusBarOverlaysWebView" value="false"/> <preference name="StatusBarBackgroundColor" value="#000000"/> </widget> 

生成的plist文件如下所示:

 <key>UISupportedInterfaceOrientations</key> <array> <string>UIInterfaceOrientationPortrait</string> </array> <key>UISupportedInterfaceOrientations¨ipad</key> <array> <string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationPortraitUpsideDown</string> <string>UIInterfaceOrientationLandscapeRight</string> </array> 

我尝试了很多解决这个bug的方法,但是其中大部分失败了。 幸运的是,我find了一个Cordova插件,它使您能够通过JavaScript成功locking屏幕方向。 在iPad上工作也是如此。

https://github.com/yoik/cordova-yoik-screenorientation

  1. 添加插件: cordova plugin add net.yoik.cordova.plugins.screenorientation
  2. 在JavaScript中使用screen.lockOrientation('portrait-primary')locking屏幕。 文档的deviceready后,一定要调用这个函数。

一点点的黑客,但一个办法来解决这个问题是通过cordova挂钩。 例如,把它放在你的hooks/before_compile目录中:

 var fs = require('fs'); var plist = './platforms/ios/YourProjectName/YourProjectName-Info.plist'; fs.exists(plist, function (exists) { if (exists) { var p = fs.readFileSync(plist, 'utf8'); p = p.replace( /<key>(UISupportedInterfaceOrientations(\~ipad)*)<\/key>[\r\n ]*<array>[\s\S]*?(?=<\/array>)/ig, "<key>$1</key>\n\t<array>\n\t\t<string>UIInterfaceOrientationLandscapeLeft</string>\n\t\t<string>UIInterfaceOrientationLandscapeRight</string>\n\t" ); fs.writeFileSync(plist, p, "utf8"); } }); 

当你为iOS( cordova build ioscordova build ios它现在应该自动改变plist。