http://blog.csdn.net/dailuwen/article/details/49887607
1、准备工作,添加插件
- 如果cordova 未添加请先执行以下命令
- 1、npm install bower -g //安装bower
- 2、bower install ngCordova //安装cordova
1.1、添加获取APP版本信息插件
- ionic plugin add cordova-plugin-app-version
2.2、添加APP自动更新相关插件
- ionic plugin add cordova-plugin-file
- ionic plugin add cordova-plugin-file-transfer
- ionic plugin add cordova-plugin-file-opener2
2、在APP一运行的时候就进行检查版本信息(在run方法添加如下代码)
- .run(function($ionicPlatform, $http, $rootScope, $ionicActionSheet, $timeout, $cordovaAppVersion,
- $ionicPopup, $ionicLoading, $cordovaFileTransfer, $cordovaFile, $cordovaFileOpener2) {
- $ionicPlatform.ready(function() {
- // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
- // for form inputs)
- if (window.cordova && window.cordova.plugins.Keyboard) {
- cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
- };
- if (window.StatusBar) {
- StatusBar.styleDefault();
- };
- //服务器上保存版本信息
- $http.get('http://localhost/app/ver.json')
- .then(function(data){
- var serverAppVersion = data.data.verInfo;//服务器 版本
- console.log("====>>服务器"+serverAppVersion);
- $cordovaAppVersion.getVersionNumber().then(function(version) {
- console.log("version=====本机>>>"+version+"====>>服务器"+serverAppVersion);
- if (version != serverAppVersion) {
- $ionicLoading.show({
- template: "已经下载:0%"
- });
- var url = "http://192.168.1.77:8080/app/android-debug.apk";
- var targetPath = "file:///mnt/sdcard/Download/android-debug.apk";
- var trustHosts = true
- var options = {};
- $cordovaFileTransfer.download(url, targetPath, options, trustHosts).then(function (result) {
- $cordovaFileOpener2.open(targetPath, 'application/vnd.android.package-archive'
- ).then(function () {
- }, function (err) {
- });
- $ionicLoading.hide();
- }, function (err) {
- alert('下载失败');
- }, function (progress) {
- $timeout(function () {
- var downloadProgress = (progress.loaded / progress.total) * 100;
- $ionicLoading.show({
- template: "已经下载:" + Math.floor(downloadProgress) + "%"
- });
- if (downloadProgress > 99) {
- $ionicLoading.hide();
- }
- })
- });
- }
- });
- });
- });
- })
3、在服务端webapp下添加以下两个文件
- 1、ver.json
- 2、Android-debug.apk