Thursday, April 3, 2014

How to add a configuration file and access it values in a PhoneGap/Cordova Application

Add a xml file to the asset/www folder and give it a name(config.xml in our case). Add settings that you need to include.

config.xml:


<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appSettings>
        <!-- QA Server-->
        <service url="http://whatsup.ai.com/service/api/" />
        <!-- Live Server-->
        <!--service url="https://user.whatsup.com/service/api/" /-->
        <version versionnumber="1.0.0.0" />
    </appSettings>

</configuration>


Code for accessing service url attribute of config file:

var serviceUrl;
function readServiceUrl() {
      $.ajax({
           url : "config.xml",
           dataType : "html",
           success : function(xmlResponse) {
           serviceUrl = $(xmlResponse).find('service').attr('url');
          
           },
           error : function(error) {
           console.log(error);
           },
           async : false
           });
}

3 comments:

  1. Hi,

    Good post. I tried this on my application. It works without issues in iphone but not in android. Any idea why?

    Priyan

    ReplyDelete
    Replies
    1. I forgot to mention that I used settings.xml as the file name since we already have a config.xml.

      Delete
    2. It should work in both platforms. I have done this countless times. Are you sure you put setting.xml file in www folder? if not make sure to input correct url to ajax request.

      Delete