Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am new to the Cordova CLI.

I need to perform the following steps programmatically via 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

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).

Can someone explain to me how to add the entry via the config.xml so that the above entry is added at compile-time?

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.


EDIT: 2/8/21 As per a comment on this question:

For anyone coming late to this, setting values in the project plist is now supported by Cordova CLI 7 and above

like image 256
Red2678 Avatar asked Mar 31 '14 18:03

Red2678


3 Answers

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

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

See the 'config-file element' section. Here's a guess as to what the relevant section of the plugin.xml will look like:

<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>

like image 77
mooreds Avatar answered Nov 09 '22 04:11

mooreds


I really like @james's solution using a Cordova hook. However, there are two issues. The docs state:

  • "we highly recommend writing your hooks using Node.js"
  • "/hooks directory is considered deprecated in favor of the hook elements in config.xml"

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

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:

  • after_prepare
  • before_compile

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

<platform name="ios">
    <hook type="after_prepare" src="scripts/my-hook.js" />
</platform>
like image 45
TachyonVortex Avatar answered Nov 09 '22 05:11

TachyonVortex


You can use the PlistBuddy utility inside a Cordova hook script to modify the *-Info.plist file.

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:

#!/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).

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. 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.

like image 32
James Avatar answered Nov 09 '22 04:11

James