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

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>

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.


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>