我不熟悉Cordova CLI。
我需要通过Cordova以编程方式执行以下步骤。
我认为我需要在项目根目录下的config.xml文件(或者"platforms“文件夹中的那个)中执行此操作。
有人能向我解释一下如何通过config.xml添加条目,以便在编译时添加上述条目吗?
我使用的是Cordova 3.3.1-0.42 (我知道它不是最新的)。我已经使我的项目,一切都很好,我只需要添加此条目添加到pList。
编辑: 2/8/21根据对此问题的评论:
支持在项目plist中设置值。
发布于 2014-04-01 22:44:10
我不认为你可以通过直接修改config.xml
来做到这一点。至少,我没有在文档中看到任何提到这一点的内容:http://cordova.apache.org/docs/en/3.3.0/config_ref_index.md.html
我认为你必须创建一个插件,因为他们可以插入plist条目:http://docs.phonegap.com/en/3.3.0/plugin_ref_spec.md.html#Plugin%20Specification
请参阅“config-file element”部分。下面是对plugin.xml
的相关部分的猜测:
<platform name="ios">
<config-file target="*-Info.plist" parent="CFBundleURLTypes">
<array>
<dict>
<key>GDLibraryMode</key>
<string>GDEnterpriseSimulation</string>
</dict>
</array>
</config-file>
</platform>
然后,您可以安装插件:cordova plugin add <your plugin name or file location>
发布于 2015-10-26 18:11:26
我真的很喜欢@james's solution使用Cordova钩子。然而,有两个问题。docs状态:
为了支持config.xml
"中的钩子元素,我们强烈建议使用Node.js"
/hooks
目录编写下面是一个使用plist NPM包的Node.js实现:
var fs = require('fs'); // nodejs.org/api/fs.html
var plist = require('plist'); // www.npmjs.com/package/plist
var FILEPATH = 'platforms/ios/.../...-Info.plist';
module.exports = function (context) {
var xml = fs.readFileSync(FILEPATH, 'utf8');
var obj = plist.parse(xml);
obj.GDLibraryMode = 'GDEnterpriseSimulation';
xml = plist.build(obj);
fs.writeFileSync(FILEPATH, xml, { encoding: 'utf8' });
};
在Cordova提供的所有钩子类型中,与您的情况相关的是:
after_prepare
before_compile
选择一种挂钩类型,然后将该挂钩添加到config.xml
文件:
<platform name="ios">
<hook type="after_prepare" src="scripts/my-hook.js" />
</platform>
发布于 2015-08-06 10:24:24
您可以在Cordova hook脚本中使用PlistBuddy实用程序来修改*-Info.plist文件。
例如,我在<project-root>/hooks/after_prepare/010_modify_plist.sh
下有以下脚本,它添加了一个字典属性并在该字典中添加了一个条目:
#!/bin/bash
PLIST=platforms/ios/*/*-Info.plist
cat << EOF |
Add :NSAppTransportSecurity dict
Add :NSAppTransportSecurity:NSAllowsArbitraryLoads bool YES
EOF
while read line
do
/usr/libexec/PlistBuddy -c "$line" $PLIST
done
true
确保将脚本设置为可执行文件(chmod +x
)。
脚本末尾的true
是因为如果要添加的键已经存在,PlistBuddy
将返回错误退出代码,并且不提供检测该键是否已经存在的方法。如果钩子脚本以错误状态退出,Cordova将报告构建错误。更好的错误处理是可能的,但实现起来很痛苦。
https://stackoverflow.com/questions/22769111
复制相似问题