Use Cordova config values
As I have been working on a iOS/Android in app purchasing plugin for Cordova, ran into the need of making some of the parameters in my code easily adjustable by developer who is going to use the plugin without the need of changing the code itself.
The best solution was sure to store these parameters, preferences as named by Cordova, inside the central
config.xml file that also Cordova (and Phonegap Build) uses for configuration of application’s behaviour. As I searched a bit around there has not been a guide to how to do this. So I jumped into the source code for both android and iOS until I found the solution which follows. I hope this will help others who are trying to achive the same result.
Using this method you can even access almost all of the other preferences values from
So basically first step is to store those values inside the
config.xml file as already mentioned. To do this your only option is to add
<preference> tags. These tags are handled by Cordova’s configuration reader in this way that either the value set for the
name of tag is known to Cordova, then it will handle it the way it knows, or they are of unkown
name so they are simply stored internally so that they can be retrieved by application or a plugin.
This is the line that does the storage for android:
String value = xml.getAttributeValue(null, "value"); action.getIntent().putExtra(name, value);
on iOS this is pretty much the same concept:
settings[[attributeDict[@"name"] lowercaseString]] = attributeDict[@"value"];
So basically all you need to do is to store the value(s) as
<preference> tags inside
config.xml file, just like:
<preference name="your-unique-key-name" value="the value you want to allow user to be able to set/change">
Not to mention that you shall do your best to choose a unique name not to conflict with other plugins, core Cordova or the application preferences itself.
Reading the vlaue in android
The value is available in android using the platfrom resources. To read out the value you need something like:
You may need to do validations after this line as the returned value may be
null, meaning that preference tag is not present in
config.xml or may be of any invalid structure.
Reading the value in iOS
This works pretty the same, what you need is to get access to the
settings dictionary which is holding the values:
[self.commandDelegate.settings objectForKey:[key lowercaseString]];
This is exaclty how one of the core plugin accesses the preferences.comments powered by Disqus