IONIC 自動更新APP版本

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


發佈了36 篇原創文章 · 獲贊 34 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章