从设备读取离子www / config.xml文件 [英] Reading ionic www/config.xml file from device
问题描述
我无法从Android设备上阅读www / config.xml文件。我尝试了两种不同的方法。首先,我尝试使用另一个线程中建议的$ http服务。当这不起作用时,我尝试使用Cordova文件插件。我这样做的原因是尝试从应用程序中的www / config.xml文件中获取版本号。以下是两种方法:
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()
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文件插件
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
推荐答案
看一下文件夹平台s / android,你可以看到文件'android_asset / www / config.xml'
不存在。
您可以尝试读取文件'/ res / xml / config.xml'
,或者您可以尝试不同的方法,例如创建 Cordova Hook 。
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代表特殊脚本可以由
应用程序和插件开发人员甚至是你自己的构建系统添加到
自定义cordova命令。
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.
更新
要获得应用的版本号,您可以使用此插件:
To get only the version number of your app you could use this plugin:
Cordova AppVersion插件
Cordova插件返回当前应用的版本号
Cordova plugin to return the version number of the current app
https://github.com/whiteoctober/cordova-plugin-app-version
这篇关于从设备读取离子www / config.xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!