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