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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22769111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 23:52:31  来源:igfitidea点击:

Add entry to iOS .plist file via Cordova config.xml

ioscordovaphonegap-pluginscordova-3cordova-plugins

提问by Red2678

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
  2. Enter the following values in the new row:
  3. Key: GDLibraryMode Type:String (default) Value:GDEnterpriseSimulation
  1. 在项目 .plist 中添加一个新行
  2. 在新行中输入以下值:
  3. :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). I have already made my project and all is fine, I just need to add this entry added to the pList.

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

采纳答案by mooreds

I don't think you can do this via straight config.xmlmodification. 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

我不认为你可以通过直接config.xml修改来做到这一点。至少,我在文档中没有看到任何提及: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.xmlwill 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>

回答by TachyonVortex

I really like @james's solutionusing a Cordova hook. However, there are two issues. The docsstate:

我真的很喜欢@james使用 Cordova 钩子的解决方案。但是,有两个问题。该文档说明:

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

Here's a Node.js implementation using the plistNPM package:

这是使用plistNPM 包的 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
  • after_prepare
  • before_compile

Choose a hook type, and then add the hook to your config.xmlfile:

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

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

回答by James

You can use the PlistBuddyutility inside a Cordova hookscript 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.shwhich 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 trueat the end of the script is because PlistBuddyreturns 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. Cordova will report a build error if the hook script exits with an error status. Better error handling is possible but a pain to implement.

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

回答by Eric Weiss

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. Type this piece into your config under the platform tag <platform name="ios">.

1.在您的应用程序中导航到您的 config.xml。在平台标签下将这部分输入到您的配置中 <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.
  1. 在您的设备上卸载并重新安装您的应用程序,您将看到您的应用程序出现在 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. There you can see the plist file that is generated for your application, which is labeled as "yourApp-info.plist". There you can see the new key and string produced in the xml layout which looks like this:

发生的情况是,当您运行 cordova prepare 命令时,您使用的是在 platform/ios 文件夹中生成的 Apple 的 Xcode SDK。在那里你可以看到为你的应用程序生成的 plist 文件,它被标记为“yourApp-info.plist”。在那里您可以看到在 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

链接到文档

回答by XML

This does seem to be possible now using the config.xml: at least some core plugin authors say so. 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. To accomplish it, they suggest executing the plugin add command with arguments, thus:

现在使用 config.xml 这似乎是可能的:至少一些核心插件作者是这样说的。例如,在Cordova 相机插件的文档中,他们讨论了 iOS 10 中的新要求,即您在 plist 中提供权限消息字符串。为了实现它,他们建议使用参数执行插件添加命令,因此:

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

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.

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

回答by Hung

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'tconfig NSCameraUsageDescriptionand NSPhotoLibraryUsageDescriptionin plugin. You need to config them in platform -> iOS project by Xcode or in *-Info.plistfile.

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

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

回答by necixy

I'm using the following in the ionic 3without 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>

回答by blakgeek

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

这是一个完整的例子

回答by Sebastien Horin

@TachyonVortex solutionseems to be the best option but was crashing down in my case. The issue was caused by an empty NSMainNibFile field that is not right converted by the plist NPM package. In the .plistfile

@TachyonVortex解决方案似乎是最好的选择,但在我的情况下崩溃了。该问题是由 plist NPM 包未正确转换的空 NSMainNibFile 字段引起的。在.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>

回答by Donovan P

Yes, it is possible!

对的,这是可能的!

I am using Cordova 9.0.0 ([email protected]).

我正在使用 Cordova 9.0.0 ([email protected])。

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