Add entry to iOS .plist file via Cordova config.xml
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>
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 inconfig.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.
These are the steps I ended up doing to enable my application to share files through itunes between devices.
1.In your application navigate to your config.xml. Type this piece into your config under the platform tag <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
- 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.
A few things, make sure cordova is up to date, and that you added the platform for ios.
npm install -g cordova
This command installs cordova.
cordova platform add ios
This command adds the platform for 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:
<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