简体   繁体   English

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

[英]Add entry to iOS .plist file via Cordova config.xml

I am new to the Cordova CLI.我是 Cordova CLI 的新手。

I need to perform the following steps programmatically via Cordova.我需要通过 Cordova 以编程方式执行以下步骤。

  1. In the project .plist add a new row在项目 .plist 中添加一个新行
  2. Enter the following values in the new row:在新行中输入以下值:
  3. Key : GDLibraryMode Type :String (default) Value :GDEnterpriseSimulation:GDLibraryMode类型:字符串(默认):GDEnterpriseSimulation

I think I need to do this in the config.xml file in my project's root (or maybe the one in the "platforms" folder).我想我需要在我的项目根目录中的 config.xml 文件中执行此操作(或者可能是“平台”文件夹中的那个)。

Can someone explain to me how to add the entry via the config.xml so that the above entry is added at compile-time?有人可以向我解释如何通过 config.xml 添加条目,以便在编译时添加上述条目吗?

I am using Cordova 3.3.1-0.42 (I know it is not the latest).我正在使用 Cordova 3.3.1-0.42(我知道它不是最新的)。 I have already made my project and all is fine, I just need to add this entry added to the pList.我已经完成了我的项目,一切都很好,我只需要将此条目添加到 pList 中即可。


EDIT: 2/8/21 As per a comment on this question:编辑:2/8/21 根据对这个问题的评论:

For anyone coming late to this, setting values in the project plist is now supported by Cordova CLI 7 and above对于迟到的任何人, Cordova CLI 7 及更高版本现在支持在项目 plist 中设置值

I don't think you can do this via straight config.xml modification.我不认为你可以通过直接的config.xml修改来做到这一点。 At least, I didn't see any mention of this in the docs: http://cordova.apache.org/docs/en/3.3.0/config_ref_index.md.html至少,我在文档中没有看到任何提及: http : //cordova.apache.org/docs/en/3.3.0/config_ref_index.md.html

I think you have to create a plugin, because they can insert plist entries: http://docs.phonegap.com/en/3.3.0/plugin_ref_spec.md.html#Plugin%20Specification我认为你必须创建一个插件,因为他们可以插入 plist 条目: http : //docs.phonegap.com/en/3.3.0/plugin_ref_spec.md.html#Plugin%20Specification

See the 'config-file element' section.请参阅“配置文件元素”部分。 Here's a guess as to what the relevant section of the plugin.xml will look like:以下是关于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>

Then you can install the plugin: cordova plugin add <your plugin name or file location>然后就可以安装插件了: cordova plugin add <your plugin name or file location>

I really like @james's solution using a Cordova hook.我真的很喜欢@james 使用 Cordova 钩子的解决方案 However, there are two issues.但是,有两个问题。 The docs state: 文档状态:

  • "we highly recommend writing your hooks using Node.js " “我们强烈建议您使用Node.js编写钩子”
  • " /hooks directory is considered deprecated in favor of the hook elements in config.xml " /hooks目录被认为已弃用,以支持config.xml中的钩子元素”

Here's a Node.js implementation using the plist NPM package:这是使用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' });

};

Of all the hook types provided by Cordova, the relevant ones for your situation are:在 Cordova 提供的所有钩子类型中,与您的情况相关的钩子类型是:

  • after_prepare
  • before_compile

Choose a hook type, and then add the hook to your config.xml file:选择挂钩类型,然后将挂钩添加到config.xml文件中:

<platform name="ios">
    <hook type="after_prepare" src="scripts/my-hook.js" />
</platform>

You can use the PlistBuddy utility inside a Cordova hook script to modify the *-Info.plist file.您可以在Cordova 挂钩脚本中使用PlistBuddy实用程序来修改 *-Info.plist 文件。

For example, I have the following script under <project-root>/hooks/after_prepare/010_modify_plist.sh which adds a dictionary property and adds an entry within that dictionary:例如,我在<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

Be sure to make the script executable ( chmod +x ).确保使脚本可执行( chmod +x )。

The true at the end of the script is because PlistBuddy returns with an error exit code if the key being added already exists, and doesn't provide a way to detect if the key already exists.脚本末尾的true是因为如果添加的密钥已经存在, PlistBuddy返回一个错误退出代码,并且不提供检测密钥是否已经存在的方法。 Cordova will report a build error if the hook script exits with an error status.如果钩子脚本以错误状态退出,Cordova 将报告构建错误。 Better error handling is possible but a pain to implement.更好的错误处理是可能的,但实现起来很痛苦。

These are the steps I ended up doing to enable my application to share files through itunes between devices.这些是我最终执行的步骤,以使我的应用程序能够通过 iTunes 在设备之间共享文件。

1.In your application navigate to your config.xml. 1.在您的应用程序中导航到您的 config.xml。 Type this piece into your config under the platform tag <platform name="ios"> .在平台标签<platform name="ios">下将这部分输入到您的配置中。

 <config-file platform="ios" target="*-Info.plist" parent="UIFileSharingEnabled">
      <true/>
  </config-file>

2. Then go to your command line tool and type: cordova prepare 2.然后转到您的命令行工具并键入:cordova prepare

  1. Uninstall and reinstall your application on your device, and you will see your app appear in itunes for you to share any files between your devices.在您的设备上卸载并重新安装您的应用程序,您将看到您的应用程序出现在 iTunes 中,供您在设备之间共享任何文件。

A few things, make sure cordova is up to date, and that you added the platform for ios.一些事情,确保cordova是最新的,并且你添加了ios平台。

npm install -g cordova

This command installs cordova.此命令安装cordova。

cordova platform add ios

This command adds the platform for ios.此命令为 ios 添加平台。

What is happening is when you run the cordova prepare command you are using Apple's Xcode SDK that is generated in the platform/ios folder.发生的情况是,当您运行 cordova prepare 命令时,您使用的是在 platform/ios 文件夹中生成的 Apple 的 Xcode SDK。 There you can see the plist file that is generated for your application, which is labeled as "yourApp-info.plist".在那里你可以看到为你的应用程序生成的 plist 文件,它被标记为“yourApp-info.plist”。 There you can see the new key and string produced in the xml layout which looks like this:在那里您可以看到在 xml 布局中生成的新键和字符串,如下所示:

 <key>UIFileSharingEnabled</key>
 <true/>

Also word of warning, my company dropped this ionic framework application into my lap a couple weeks ago (with a really short deadline).还有一个警告,我的公司几周前将这个离子框架应用程序放到了我的腿上(截止日期非常短)。 Everything I am telling you is based on couple weeks of learning.我告诉你的一切都是基于几周的学习。 So this may not be the best practice, but I hope it helps someone out.所以这可能不是最佳实践,但我希望它可以帮助某人。

Edit编辑

Link to the docs 链接到文档

This does seem to be possible now using the config.xml: at least some core plugin authors say so.现在使用 config.xml 这似乎是可能的:至少一些核心插件作者是这样说的。 For instance, in the docs for the Cordova Camera Plugin , they discuss the new requirement in iOS 10 that you provide a permission message string in the plist.例如,在Cordova 相机插件的文档中,他们讨论了 iOS 10 中的新要求,即您在 plist 中提供权限消息字符串。 To accomplish it, they suggest executing the plugin add command with arguments, thus:为了实现它,他们建议使用参数执行插件添加命令,因此:

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

This has the result that you not only get a new <plugin> added to config.xml, but it has a <variable> child:这导致您不仅将一个新的<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>

Which then seems to correlate to the new keys in my info.plist, perhaps somehow passing the values at runtime?然后似乎与我的 info.plist 中的新键相关,也许在运行时以某种方式传递值?

  <key>NSCameraUsageDescription</key>
  <string/>
  <key>NSPhotoLibraryUsageDescription</key>
  <string/>

I'd be lying if I said that I know exactly how it works, but it seems to point the way.如果我说我确切地知道它是如何工作的,我会撒谎,但它似乎指明了方向。

UPDATE: for people want to use camera with iOS >= 10. This mean, by normal you can config in plugin as:更新:对于想要在 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>

But for now, you can't config NSCameraUsageDescription and NSPhotoLibraryUsageDescription in plugin.但是现在,你不能在插件中配置NSCameraUsageDescriptionNSPhotoLibraryUsageDescription You need to config them in platform -> iOS project by Xcode or in *-Info.plist file.您需要在平台 -> iOS 项目中通过 Xcode 或在*-Info.plist文件中配置它们。

Since iOS 10 it's mandatory to add a NSCameraUsageDescription and NSPhotoLibraryUsageDescription in the info.plist.从 iOS 10 开始,必须在 info.plist 中添加 NSCameraUsageDescription 和 NSPhotoLibraryUsageDescription。

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

I'm using the following in the ionic 3 without any additional plugin or imports and I think this could be helpful for others:我在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>

You can set the display name in app's plist by directly editing the ios.json in the plugins directory.可以直接编辑plugins目录下的ios.json,在app的plist中设置显示名称。

Adding the following to the config_munge.files section of the ios.json file will do the trick and it will be maintained even when using the CLI.将以下内容添加到 ios.json 文件的 config_munge.files 部分即可解决问题,即使使用 CLI 也会对其进行维护。

"*-Info.plist": {

    "parents": {
        "CFBundleDisplayName": [
            {
                "xml": "<string>RevMob Ads Cordova Plugin Demo</string>",
                "count": 1
            }
        ]
    }
}

Here's a complete example这是一个完整的例子

@TachyonVortex solution seems to be the best option but was crashing down in my case. @TachyonVortex解决方案似乎是最好的选择,但在我的情况下崩溃了。 The issue was caused by an empty NSMainNibFile field that is not right converted by the plist NPM package.该问题是由 plist NPM 包未正确转换的空 NSMainNibFile 字段引起的。 In the .plist file.plist文件中

    <key>NSMainNibFile</key>
    <string></string>
    <key>NSMainNibFile~ipad</key>
    <string></string>

is converted to:转换为:

    <key>NSMainNibFile</key>
    <string>NSMainNibFile~ipad</string>

I fixed it with by adding to the script:我通过添加到脚本来修复它:

    obj.NSMainNibFile = '';
    obj['NSMainNibFile~ipad'] = '';

The script finally looks like (scripts/my-hook.js):脚本最终看起来像 (scripts/my-hook.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';
    obj.NSMainNibFile = '';
    obj['NSMainNibFile~ipad'] = '';

    xml = plist.build(obj);
    fs.writeFileSync(FILEPATH, xml, { encoding: 'utf8' });

};

and config.xml:和 config.xml:

<platform name="ios">
    <hook type="before_build" src="scripts/my-hook.js" />
</platform>

I have used this plugin to solve the problem, maybe it can help you :我已经用这个插件解决了这个问题,也许它可以帮助你:

https://www.npmjs.com/package/cordova-plugin-queries-schemeshttps://www.npmjs.com/package/cordova-plugin-queries-schemes

Yes, it is possible!对的,这是可能的!

I am using Cordova 9.0.0 (cordova-lib@9.0.1).我正在使用 Cordova 9.0.0 (cordova-lib@9.0.1)。

For example this is the configuration file that I used to insert new string value into Info.plist :例如,这是我用来将新字符串值插入 Info.plist 的配置文件:

<platform name="ios">
    <edit-config file="*-Info.plist" mode="merge" target="NSMicrophoneUsageDescription">
        <string>My awesome app wish to hear your awesome voice through Microphone. Not for fancy stuff, just want to hear you.</string>
    </edit-config>
    <edit-config file="*-Info.plist" mode="merge" target="---Key configuration---">
        <string>---string value---</string>
    </edit-config>
</platform>

After that, don't forget to rebuild your staging file by running this two command in your terminal :之后,不要忘记通过在终端中运行这两个命令来重建临时文件:

cordova platform rm ios
cordova platform add ios

To confirm the change, you can check the newly generated .plist file by opening them with xCode.要确认更改,您可以通过使用 xCode 打开它们来检查新生成的 .plist 文件。

-Info.plist file located at : -Info.plist 文件位于:

./platform/ios/[your app name]/[your app name]-Info.plist

If you are trying to modify a .plist in a native iOS plugin with a <config-file> tag in your plugin.xml , here is what you need to do:如果您尝试在plugin.xml使用<config-file>标记修改本机 iOS 插件中的.plist ,则需要执行以下操作:

  1. Make sure your .plist is xml, not binary!确保您的.plist是 xml,而不是二进制文件! You can use plutil to convert a binary .plist into xml, and commit it to version control.您可以使用plutil将二进制.plist转换为 xml,并将其提交给版本控制。

    plutil -convert xml1 Info.plist

  2. The instructions for <config-file> indicate that target= is relative to the generated xcode project at platforms/ios/<project>/ , but I found that I needed to prepend a wildcard character to my path to get it to work: <config-file>说明表明target=是相对于在platforms/ios/<project>/生成的 xcode 项目的,但我发现我需要在我的路径前添加一个通配符以使其工作:

    target="*/Resources/MyResources.bundle/Info.plist"

  3. If you want to add a key at the top level of the .plist , you need to set parent equal to the key name, and then nest a <string> tag with the value.如果你想在.plist的顶层添加一个键,你需要设置 parent 等于键名,然后用值嵌套一个<string>标签。 Using an <array> or <dict> as any examples show will cause these keys to be nested under parent .如任何示例所示,使用<array><dict>将导致这些键嵌套parent

Here is a complete example that works for me for adding multiple top level properties:这是一个完整的示例,适用于我添加多个顶级属性:

<platform name="ios">
    <config-file target="*/Resources/MyResources.bundle/Info.plist" parent="MyDistribution">
        <string>Cordova</string>
    </config-file>
    <config-file target="*/Resources/MyResources.bundle/Info.plist" parent="MyVersion">
        <string>3.2.0</string>
    </config-file>
</platform>

I prefer the after_prepare hook for bigger projects or if you have multiple plugins using the same permissions.对于较大的项目,或者如果您有多个使用相同权限的插件,我更喜欢 after_prepare 钩子。 But you can always go the simple way:但你总是可以走简单的路:

simply: - remove the plugin that requires the desired permission - add it again with --save - in config.xml, the plugin now has a new variable with a blank description that you can fill in - now build ios with -- release and they will be set.简单地: - 删除需要所需权限的插件 - 使用 --save 再次添加 - 在 config.xml 中,插件现在有一个带有空白描述的新变量,您可以填写 - 现在使用 -- release 和他们将被设置。

you just need following steps 1.您只需要执行以下步骤 1。

Go to Project navigator Select the target Click on info from tab option other option are build setting build phase you will see key type value When you point on any key name you will find + and - sign click on the + sign write Key: GDLibraryMode in key section Type:String in tyoe section Value:GDEnterpriseSimulation in value section转到项目导航器选择目标单击选项卡中的信息选项其他选项是构建设置构建阶段您将看到密钥类型值当您指向任何密钥名称时,您会发现+和-符号单击+符号写入Key: GDLibraryMode关键部分Type:String tyoe 部分中的Type:String Value:GDEnterpriseSimulation值部分中的Value:GDEnterpriseSimulation

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Cordova ios 添加扩展 - 错误:找不到 -Info.plist 文件或 config.xml 文件 - Cordova ios adding extension - Error: could not find -Info.plist file, or config.xml file 如何通过 config.xml 将 NSPhotoLibraryUsageDescription 添加到 plist 文件? - How do I add the NSPhotoLibraryUsageDescription to the plist file via the config.xml? Cordova 3.2 ios添加插件“哪个config.xml? 它在哪里?” - Cordova 3.2 ios add plugin “Which config.xml? Where is it?” PhoneGap:修改config.xml以向Info.plist离子iOS添加属性 - PhoneGap: modify config.xml to add properties to Info.plist ion iOS 通过phonegap应用中的config.xml编辑info.plist文件? - Editing the info.plist file via config.xml in phonegap app? Cordova / Phonegap在iOS上的config.xml中忽略全屏首选项 - Cordova/Phonegap ignores fullscreen preference in config.xml on iOS 错误:找不到-Info.plist文件或config.xml文件 - Error: could not find -Info.plist file, or config.xml file 如何从我的iPhone应用程序的config.xml文件中的info.plist中读取密钥的值 - How to read the value for a key from the info.plist in an config.xml file in my iPhone app 尽管 Ionic 应用程序在 config.xml 中,但它们并未在 info plist 文件中显示各种权限 - Ionic app not showing various permissions in info plist file despite them being in config.xml 科尔多瓦:如何设置特定于平台的config.xml文件,科尔多瓦构建后该文件不会被覆盖? - Cordova: How to set Platform-specific config.xml file which is not overwritten after cordova build?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM