iPhone屏幕方向在cordova 3.5.0中无法更改。

4

当我在iPhone上旋转屏幕时,我的应用程序始终保持竖屏,没有任何变化。然而,对于iPad来说,它可以顺利地旋转并改变方向。

如何使我的应用程序在我转动iPhone时改变屏幕方向?

config.xml中,我有:

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

我唯一能找到的解决方案是在cordova代码中更改MainViewController.m中的shouldAutorotateToInterfaceOrientation方法。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
--    return [super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
++    return true;
}

但这对我来说似乎是一个丑陋的解决方案,我真的很希望能找到正常的方法来解决这个问题。

理想情况下,对于iPad,所有方向都可以,对于iPhone,除了portraitUpsideDown外,其他方向都可以,但我不知道我是否有权利做梦。

还有其他人在使用iPhone和cordova时遇到类似的问题吗?我已经搜索了几天,除了在cordova代码中找到这个黑客方法之外,什么也找不到。


1
这是一个JavaScript的解决方案:https://dev59.com/nGgu5IYBdhLWcg3w5K9a#28203969 - Greg
3个回答

4
在广泛寻找解决方案之后,我不幸地得出结论,这是 Cordova 问题。

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

需要将代码实际更改iOS配置文件(/platform/ios/~app_name~/~app_name~.plist)中的UISupportedInterfaceOrientations键,以包括以下值:

  • "UIInterfaceOrientationLandscapeLeft";
  • "UIInterfaceOrientationLandscapeRight";
  • "UIInterfaceOrientationPortrait"; 以及(我认为)
  • "UIInterfaceOrientationPortraitUpsideDown"

不幸的是,更改config.xml文件并不会更改plist文件,这非常令人恼火。希望这个问题很快得到解决,根据在线线程的判断,这已经浪费了很多人寻找解决方案的时间。

我发现最简单的解决方法是手动更改~app_name~.plist文件,以包括上述值。我一直在Xcode中进行操作,这使得操作非常容易。

希望这可以帮到您。如果有更好的解决方案或Cordova修复此疏漏,请告诉我。


只是作为旁注,值得记录的是哪些其他config.xml选项不起作用。 如果还有其他,请发布以避免浪费更多时间...... - FrodmanG
1
我不喜欢修改Cordova生成的代码。这里有一个JavaScript解决方案。https://dev59.com/nGgu5IYBdhLWcg3w5K9a#28203969 - Greg

2
我在XCode中创建了一个构建阶段,使用PListBuddy,这样每当你运行cordova build ios时,它就会运行构建阶段并更新PList文件。好处是,如果你更改了config.xml并更改了PList,构建阶段会介入并确保该值存在。
我从这里的被接受的答案中得到了灵感:Is there a way of automatically writing custom values to the bundle's .plist during a build phase?
#Grabs info from plist
plist=$SRCROOT"/"$INFOPLIST_FILE
orientations=`/usr/libexec/PlistBuddy -c "Print :UISupportedInterfaceOrientations" "$plist"`

#And changes it before writing out the plist again
if [ "$orientations" ]
then
/usr/libexec/PlistBuddy -c "Delete :UISupportedInterfaceOrientations array" "$plist"
fi
/usr/libexec/PlistBuddy -c "Add :UISupportedInterfaceOrientations array" "$plist"
/usr/libexec/PlistBuddy -c "Add :UISupportedInterfaceOrientations:0 string \"UIInterfaceOrientationPortrait\"" "$plist"
/usr/libexec/PlistBuddy -c "Add :UISupportedInterfaceOrientations:0 string \"UIInterfaceOrientationPortraitUpsideDown\"" "$plist"
/usr/libexec/PlistBuddy -c "Add :UISupportedInterfaceOrientations:0 string \"UIInterfaceOrientationLandscapeLeft\"" "$plist"
/usr/libexec/PlistBuddy -c "Add :UISupportedInterfaceOrientations:0 string \"UIInterfaceOrientationLandscapeRight\"" "$plist"

作为一个旁注,我们不应该为了两全其美而费尽心思,但无论如何。

这真的很酷,谢谢。我一直在做一些烦人的字符串替换,以尽可能自动化我的构建,但这个方法更好。 - standup75

0

我创建了一个钩子文件,参考了这里的内容: https://dev59.com/CIHba4cB1Zd3GeqPUKYO#27063466

#!/usr/bin/env node

var fs = require('fs');
var plist = 'platforms/ios/<app-name>/<app-name>-Info.plist';

var iphoneModes = [
    "UIInterfaceOrientationLandscapeLeft",
    "UIInterfaceOrientationLandscapeRight",
    "UIInterfaceOrientationPortrait"
];

var ipadModes = [
    "UIInterfaceOrientationLandscapeLeft",
    "UIInterfaceOrientationLandscapeRight"
];

function getOrientationModeStr(modes) {
    var s = "<key>$1</key>\n\t<array>\n\t";
    modes.forEach(function(mode, index) {
        s += "\t<string>"+mode+"</string>\n\t";
    });
    return s;
}

if (fs.existsSync(plist)) {
    var p = fs.readFileSync(plist, 'utf8');
    // replace iphone modes
    p = p.replace(
        /<key>(UISupportedInterfaceOrientations)<\/key>[\r\n ]*<array>[\s\S]*?(?=<\/array>)/ig,
        getOrientationModeStr(iphoneModes)
    );
    // replace ipad modes
    p = p.replace(
        /<key>(UISupportedInterfaceOrientations~ipad)<\/key>[\r\n ]*<array>[\s\S]*?(?=<\/array>)/ig,
        getOrientationModeStr(ipadModes)
    );
    fs.writeFileSync(plist, p, "utf8");
}

希望这有所帮助。


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