Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading ionic www/config.xml file from device

I am having trouble reading the www/config.xml file from Android device. I tried two different approaches. First, I tried using the $http service which was recommended in another thread to do. When that did not work I then tried to use the Cordova file plugin. The reason I am doing this is to try to get the version number from the www/config.xml file in the app. Below are both approaches:

1) $http.get()

 url = 'file:///android_asset/www/config.xml'
 $http.get(url)
    .then ((data) ->
      versionNumber = data.data.match(/<widget\s.*?\sversion=['"]([^'"]+)['"]/)
      verNum = versionNumber[1]
    ), (err) ->
    # {"data": null, "status": 0, .....}
      alert JSON.stringify err

2) Cordova file plugin

   if ionic.Platform.isAndroid()
    path = 'file:///android_asset/www/'
    file = 'config.xml'
    $cordovaFile.readAsText(path, file)
      .then ((data) ->
        alert JSON.stringify data
        return
      ), (error) ->
        # I alert { code: 5 } which is an ENCODING_ERR
        alert JSON.stringify error
like image 570
eNddy Avatar asked Feb 17 '15 20:02

eNddy


2 Answers

Take a look to the folder platforms/android and you can see that the file 'android_asset/www/config.xml' don't exists. You could try reading the file '/res/xml/config.xml' or you could try a different approach for example creating a Cordova Hook.

Cordova Hooks:

Cordova Hooks represent special scripts which could be added by application and plugin developers or even by your own build system to customize cordova commands.

You could adapt the replace text hook from this article or this build number hook based on the same hook.

UPDATE

To get only the version number of your app you could use this plugin:

Cordova AppVersion plugin

Cordova plugin to return the version number of the current app

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

Is available also with ngCordova

like image 193
manzapanza Avatar answered Oct 11 '22 22:10

manzapanza


ngCordova has a great set of plugins when working with ionic. To get the app version use this one: http://ngcordova.com/docs/plugins/appVersion/

I wanted to display the version number in my splash screen, so I used their example and added an event:

Inside $ionicPlatform.ready callback:

$cordovaAppVersion.getVersionNumber().then(function (version) {
    $rootScope.$broadcast("appVersionResolved", { version: appVersion });
}

In the splash screen controller:

$rootScope.$on("appVersionResolved", function(event, args) {
    $scope.appVersion = args.version;
});
like image 32
Dan Pincu Avatar answered Oct 11 '22 21:10

Dan Pincu