通过Cordova config.xml向iOS .plist文件添加条目

Red*_*678 78 ios phonegap-plugins cordova cordova-3 cordova-plugins

我是Cordova CLI的新手.

我需要通过Cordova以编程方式执行以下步骤.

  1. 在项目.plist中添加一个新行
  2. 在新行中输入以下值:
  3. :GDLibraryMode 类型:字符串(默认):GDEnterpriseSimulation

我想我需要在项目根目录中的config.xml文件中执行此操作(或者可能是"platforms"文件夹中的那个).

有人可以向我解释如何通过config.xml添加条目,以便在编译时添加上述条目吗?

我正在使用Cordova 3.3.1-0.42(我知道它不是最新的).我已经完成了我的项目,一切都很好,我只需要将这个条目添加到pList中.

moo*_*eds 64

我不认为你可以通过直接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>
Run Code Online (Sandbox Code Playgroud)

然后你可以安装插件: cordova plugin add <your plugin name or file location>

  • @mooreds,Cordova/PhoneGap文档具有误导性.我发现密钥应该在`parent`中指定,而值部分应该是内部XML.在这种特殊情况下,它应该如下:<config-file target ="* - Info.plist"parent ="GDLibraryMode"> <string> GDEnterpriseSimulation </ string> </ config-file> (6认同)
  • 这个插件工作得很好https://github.com/leecrossley/cordova-plugin-transport-security (4认同)
  • 作为参考,从Cordova CLI 7+开始,可以通过&lt;config-file&gt;元素在config.xml中完成此操作-有关其他操作的详细信息,请参见其他答案。 (2认同)

Tac*_*tex 43

我非常喜欢@james 使用Cordova钩子的解决方案.但是,有两个问题.该文档的状态:

  • "我们强烈建议您使用Node.js编写钩子"
  • " /hooks目录被认为已弃用,而有利于config.xml"

这是使用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' });

};
Run Code Online (Sandbox Code Playgroud)

在Cordova提供的所有钩子类型中,与您的情况相关的是:

  • after_prepare
  • before_compile

选择一个钩子类型,然后将钩子添加到您的config.xml文件中:

<platform name="ios">
    <hook type="after_prepare" src="scripts/my-hook.js" />
</platform>
Run Code Online (Sandbox Code Playgroud)


Jam*_*mes 32

您可以在Cordova钩子脚本中使用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
Run Code Online (Sandbox Code Playgroud)

一定要使脚本可执行(chmod +x).

true在脚本的结束是因为PlistBuddy一个错误退出代码返回如果该键被添加已经存在,并且不提供一种方法来检测,如果该键已经存在.如果钩子脚本以错误状态退出,Cordova将报告构建错误.更好的错误处理是可能的,但实施起来很痛苦.


小智 21

这些是我最终要做的步骤,以使我的应用程序能够通过设备之间的iTunes共享文件.

1.在您的应用程序中,导航到config.xml.在平台标签下的配置中键入此部分 <platform name="ios">.

 <config-file platform="ios" target="*-Info.plist" parent="UIFileSharingEnabled">
      <true/>
  </config-file>
Run Code Online (Sandbox Code Playgroud)

2.然后转到命令行工具并键入:cordova prepare

  1. 在您的设备上卸载并重新安装您的应用程序,您将看到您的应用程序显示在iTunes中,以便您在设备之间共享任何文件.

一些事情,确保cordova是最新的,并且您添加了ios的平台.

npm install -g cordova
Run Code Online (Sandbox Code Playgroud)

此命令安装cordova.

cordova platform add ios
Run Code Online (Sandbox Code Playgroud)

此命令添加ios平台.

当您运行cordova prepare命令时,您正在使用在platform/ios文件夹中生成的Apple的Xcode SDK.在那里,您可以看到为您的应用程序生成的plist文件,标记为"yourApp-info.plist".在那里,您可以看到xml布局中生成的新键和字符串,如下所示:

 <key>UIFileSharingEnabled</key>
 <true/>
Run Code Online (Sandbox Code Playgroud)

同样警告,我的公司几周前将这种离子骨架应用程序放入我的膝盖上(截止日期非常短).我告诉你的一切都是基于几个星期的学习.所以这可能不是最好的做法,但我希望它可以帮助别人.

  • 还需要其他任何工作吗?我正在使用Cordova 6.2(最新产品),即使我在config.xml中添加了条目,我也没有在plist中看到它.您是否可以使用Cordova Custom Config插件(https://github.com/dpa99c/cordova-custom-config)? (4认同)

XML*_*XML 13

现在使用config.xml似乎可以做到这一点:至少有一些核心插件作者这样说.例如,在Cordova Camera Plugin的文档中,他们讨论了iOS 10中您在plist中提供权限消息字符串的新要求.为了实现它,他们建议使用参数执行plugin add命令,因此:

cordova plugin add cordova-plugin-camera --variable CAMERA_USAGE_DESCRIPTION="My App would like to access your camera, to take photos of your documents."

这样,您不仅可以获得一个新<plugin>添加到config.xml的结果,而且还有一个<variable>子项:

<plugin name="cordova-plugin-camera" spec="~2.3.0">
    <variable name="CAMERA_USAGE_DESCRIPTION" value="My App would like to access your camera, to take photos of your documents." />
</plugin>
Run Code Online (Sandbox Code Playgroud)

这似乎与我的info.plist中的新键相关,也许以某种方式在运行时传递值?

  <key>NSCameraUsageDescription</key>
  <string/>
  <key>NSPhotoLibraryUsageDescription</key>
  <string/>
Run Code Online (Sandbox Code Playgroud)

如果我说我确切知道它是如何工作的,我会说谎,但它似乎指明了方向.


小智 9

更新:对于想要使用iOS> = 10的相机的人.这意味着,通常你可以在插件中配置为:

 <!-- ios -->
 <platform name="ios">

     <config-file target="*-Info.plist" parent="NSLocationWhenInUseUsageDescription">
         <string></string>
     </config-file>
     <config-file target="*-Info.plist" parent="NSCameraUsageDescription">
         <string></string>
     </config-file>
      <config-file target="*-Info.plist" parent="NSPhotoLibraryUsageDescription">
         <string></string>
     </config-file>

 </platform>
Run Code Online (Sandbox Code Playgroud)

但现在,你不能配置NSCameraUsageDescriptionNSPhotoLibraryUsageDescription在插件.您需要在平台 - > iOS项目中通过Xcode或*-Info.plist文件配置它们.

从iOS 10开始,必须在info.plist中添加NSCameraUsageDescription和NSPhotoLibraryUsageDescription.

了解更多信息:https://www.npmjs.com/package/cordova-plugin-camera


nec*_*ixy 7

我在ionic 3中使用了以下内容,而没有任何其他插件或导入,我认为这可能对其他人有帮助:

<platform name="ios">
    <edit-config file="*-Info.plist" mode="merge" target="NSLocationWhenInUseUsageDescription">
        <string>Location is required so we can show you your nearby projects to support.</string>
    </edit-config>
    <edit-config file="*-Info.plist" mode="merge" target="NSCameraUsageDescription">
        <string>Camera accesss required in order to let you select profile picture from camera.</string>
    </edit-config>
    <edit-config file="*-Info.plist" mode="merge" target="NSPhotoLibraryUsageDescription">
        <string>Photo library accesss required in order to let you select profile picture from gallery / library.</string>
    </edit-config>
</platform>
Run Code Online (Sandbox Code Playgroud)


bla*_*eek 5

可以直接编辑plugins目录下的ios.json,在app的plist中设置显示名称。

将以下内容添加到 ios.json 文件的 config_munge.files 部分即可解决问题,即使使用 CLI 也会对其进行维护。

"*-Info.plist": {

    "parents": {
        "CFBundleDisplayName": [
            {
                "xml": "<string>RevMob Ads Cordova Plugin Demo</string>",
                "count": 1
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

这是一个完整的例子

  • 这个答案有两个问题。首先,当运行 `cordova platform remove ios` 和 `cordova platform add ios` 时,对 `/plugins/ios.json` 的任何手动编辑都将丢失。其次,@Red2678 要求“通过 Cordova 以编程方式执行编辑......以便在编译时添加新条目”的解决方案。 (2认同)