Is there a cordova plugin to read values from conf

2019-01-27 15:32发布

I wish to read these values from my Cordova/PhoneGap application's config.xml at runtime:

  • name
  • copyright
  • description

However, was surprised to see there is no 'Config' feature in the API reference guide: http://cordova.apache.org/docs/en/3.4.0/index.html

I've resorted to writing my own function that reads and parses this file manually, however I feel like there must be an (existing) better way.

Should developers be parsing config.xml manually to extract necessary info, or is there an existing plugin that can be used to do this?

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-27 15:38

For the ones who don't want to mess with the xhr queries, there are two plugins you can use:

1 plugin-buildinfo (just for Android and IOS but sooo great)

2 plugin-app-version (lighter but supports more platforms)

To start quickly with the second one, all you need to do is adding the plugin into your project:

cordova plugin add https://github.com/whiteoctober/cordova-plugin-app-version.git

and calling where you want as follows:

cordova.getAppVersion(function (version) {
    alert(version);
});
查看更多
虎瘦雄心在
3楼-- · 2019-01-27 15:46

You could use following code on iOS, WP7, WP8, Windows8, and probably Ubuntu

function readConfig() {
    var xhr = new XMLHttpRequest();
    xhr.addEventListener("load", function () {
        var parser = new DOMParser();
        var doc = parser.parseFromString(xhr.responseText, "application/xml");
        alert("Description : " + 
              doc.getElementsByTagName("description").item(0).textContent);
    });
    xhr.open("get", "../config.xml", true);
    xhr.send();
}

for the Android you have to change path to file from "../config.xml" to "../../android_res/xml/config.xml"

Taken from Cordova mailing where discussed answer: https://www.mail-archive.com/dev@cordova.apache.org/msg14313.html

Also there not-official plugin for reading configuration: https://github.com/apache/cordova-labs/tree/cdvtest/cordova-plugin-appsettings

查看更多
倾城 Initia
4楼-- · 2019-01-27 16:00

You could use following Cordova plugin:

cordova plugin add cordova-plugin-customconfigparameters

Add your Custom Parameters in Config.xml as preference tags:

<preference name="name" value="Ibrahim"/>
<preference name="copyright" value="Direct Direction 2017"/>
<preference name="description" value="Information Technology"/>

Note: be sure the preference name should be a small letter (to work on IOS).

Then in your page Get a key's value from the Config.xml using the following script:

var paramkeyArray=["name","copyright","description"];
CustomConfigParameters.get(function(configData){
    console.log(configData.name);
    console.log(configData.copyright);
    console.log(configData.description);
},function(err){
  console.log(err);
},paramkeyArray);

For more detail see https://www.npmjs.com/package/cordova-plugin-customconfigparameters

查看更多
登录 后发表回答