【问题标题】:Cordova 5 build command is deleting iOS device orientation settingsCordova 5 build 命令正在删除 iOS 设备方向设置
【发布时间】:2015-06-15 15:12:51
【问题描述】:

在 Cordova 5.1.1 中,当执行“cordova build ios”时,之前在 XCode 项目中选择的任何设备方向设置都将被删除,而方向设置复选框则未被选中。

虽然“方向”配置首选项可能会提供一种强制方向的方法,但我需要能够为 iPad 和 iPhone 设置不同的方向首选项。

所有以前的 Cordova 版本(低于 5)都尊重这些设置。有什么想法吗?

使用 XCode 6.3.2。

【问题讨论】:

    标签: ios iphone xcode cordova cordova-5.0.0


    【解决方案1】:

    编辑:

    根据@Abhinav Gujjar,导致cordova prepare 覆盖对.plist 中的方向设置所做的手动更改的问题已得到修复。但是,AFAIK 仍然无法在 config.xml 中为 iPad 和 iPhone 设置不同的方向偏好,所以下面的答案是正确的。

    更新:

    我创建了插件cordova-custom-config,它封装了下面的钩子,意味着可以在 config.xml 中定义特定于平台的自定义配置块(例如这些方向设置)。所以你可以使用插件而不需要手动创建下面的钩子。


    这是在 Cordova 5.0.0 CLI - see here 中引入的。

    与此同时,我一直在使用 after_prepare 挂钩作为解决方法。只需将以下内容放入<your_project>/hooks/after_prepare/some_file.js 并根据需要更改方向设置:

    #!/usr/bin/env node
    
    // Set support for all orienations in iOS .plist - workaround for this cordova bug: https://issues.apache.org/jira/browse/CB-8953
    var platforms = process.env.CORDOVA_PLATFORMS.split(',');
    platforms.forEach(function(p) {
        if (p == "ios") {
            var fs = require('fs'),
                plist = require('plist'),
                xmlParser = new require('xml2js').Parser(),
                plistPath = '',
                configPath = 'config.xml';
            // Construct plist path.
            if (fs.existsSync(configPath)) {
                var configContent = fs.readFileSync(configPath);
                // Callback is synchronous.
                xmlParser.parseString(configContent, function (err, result) {
                    var name = result.widget.name;
                    plistPath = 'platforms/ios/' + name + '/' + name + '-Info.plist';
                });
            }
            // Change plist and write.
            if (fs.existsSync(plistPath)) {
                var pl = plist.parseFileSync(plistPath);
                configure(pl);
                fs.writeFileSync(plistPath, plist.build(pl).toString());
            }
            process.exit();
        }
    });
    function configure(plist) {
        var iPhoneOrientations = [
            'UIInterfaceOrientationLandscapeLeft',
            'UIInterfaceOrientationLandscapeRight',
            'UIInterfaceOrientationPortrait',
            'UIInterfaceOrientationPortraitUpsideDown'
        ];
        var iPadOrientations = [
                'UIInterfaceOrientationLandscapeLeft',
                'UIInterfaceOrientationLandscapeRight',
                'UIInterfaceOrientationPortrait',
                'UIInterfaceOrientationPortraitUpsideDown'
        ];
        plist["UISupportedInterfaceOrientations"] = iPhoneOrientations;
        plist["UISupportedInterfaceOrientations~ipad"] = iPadOrientations;
    }
    

    注意:如果您还没有 plist 和 xml2js 节点模块,则需要安装它们。

    【讨论】:

    • 这个解决方案非常有效,戴夫。起初我在没有 sudo 的情况下安装了 plist 和 xml2js,但我遇到了问题。一旦我卸载了两者并使用 sudo 重新安装,一切都运行良好。再次感谢。
    【解决方案2】:

    如果您愿意,您可以在 JS 端以编程方式进行。

    对于 iOS,可以通过定义 窗口上的javascript回调

    /** 
    // @param {Number} degree - UIInterfaceOrientationPortrait: 0,
    // UIInterfaceOrientationLandscapeRight: 90, 
    // UIInterfaceOrientationLandscapeLeft: -90,     
    // UIInterfaceOrientationPortraitUpsideDown: 180
    * @returns {Boolean} Indicating if rotation should be allowed.
    */
    function shouldRotateToOrientation(degrees) {
         return true;
    }
    

    在您的 config.xml 文件中设置允许的方向

    <platform name="ios">
        <preference name="Orientation" value="all" />
    </platform>
    

    并添加shouldRotateToOrientation(degrees)

    onDeviceReady: function() {
        app.receivedEvent('deviceready');
        window.shouldRotateToOrientation = function(degrees) {
            //if device an iPad ?
            if ( navigator.userAgent.match(/iPad/i) ) {
                return true;
            } 
            //else if device an iPhone ?
            else if (navigator.userAgent.match(/iPhone/i)) {
                if (degrees == 0) { //orientation is portrait
                  return true;
                }
                return false; //refuse all other orientations for iPhone
            }
            return true;
        };
    }
    

    【讨论】:

    • 你把这段代码扔到哪里去了?就在 AngularJS 应用的 index.html 文件中?
    • 非常感谢您的跟进。我玩得很开心。我在我的应用程序中升级了 Cordova 的 iOS 插件版本,它破坏了方向旋转 - 至少在此之前。
    • 对我有用 - 有一件事,也许你可以把你的 ifs 放在外面,即根据设备设置回调,这样它只匹配一次 - 非常感谢顺便说一句,这是一个沮丧
    【解决方案3】:

    此问题已修复,您现在可以在 config.xml 中将方向指定为“全部”

    <platform name="ios">
          <preference name="Orientation" value="all" />
    </platform>
    

    Docs

    【讨论】:

    • 可能已经修复了导致方向首选项被覆盖的错误,但最初的问题是“我需要能够为 iPad 和 iPhone 设置不同的方向首选项”和 AFAIK,这仍然是不可能的使用 config.xml 中的设置来做
    • 你是对的 - OP 确实需要不同的设备类型规格。我来到这里是因为它把我锁在肖像上。猜猜我很兴奋,这是固定的。
    • 不过,很高兴知道该错误已得到修复 - 感谢您的更新 :-)
    • 不幸的是,这在 cordova 5.4 中被破坏了:issues.apache.org/jira/browse/CB-9975
    猜你喜欢
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多