ionic2項目自動升級APP

一、所須要的插件: cordova plugin add https://github.com/whiteoctober/cordova-plugin-app-version.git // 獲取APP版本 cordova plugin add org.apache.cordova.file // 文件系統 cordova plugin add org.apache.cordova.file-transfer //文件傳輸系統 cordova plugin add https://github.com/pwlin/cordova-plugin-file-opener2 //文件打開系統android

ionic-native須要升級到1.3.20以上;git

二、ts文件調用 2.1版本號讀取:github

import {AppVersion} from "ionic-native/dist/index";
getVerNumber(){
        AppVersion.getVersionNumber().then((version)=> {
            this.versionNo = version;
        });

    }

2.2 版本下載和版本打開web

import {Transfer, FileOpener} from "ionic-native/dist/index";
upgradeApp(){
            const fileTransfer = new Transfer();

            let uploading = this.loadingCtrl.create({
                 content: "安裝包正在下載...",
                 dismissOnPageChange: false
            });


        var url = "http://210.21.199.68:8080/web/data/commu.apk"; //能夠從服務端獲取更新APP的路徑
            var targetPath = "/sdcard/Download/commun.apk"; //APP下載存放的路徑,可使用cordova file插件進行相關配置
            // var options = {};
             uploading.present();

            fileTransfer.onProgress((event) => {
                //進度,這裏使用文字顯示下載百分比
                //  setTimeout(function () {
                var downloadProgress = (event.loaded / event.total) * 100;
                uploading.setContent("已經下載:" + Math.floor(downloadProgress) + "%");

                if (downloadProgress > 99) {
                    uploading.destroy();
                }
                // },10000);

                /* setTimeout(() => {
                 uploading.dismiss();
                 }, 10000);*/

            });

            //url爲服務端地址
            //targetPath爲設備上的地址
            fileTransfer.download(url, targetPath,true).then(
                (result) =>{
                         uploading.destroy();
                         FileOpener.open(targetPath, 'application/vnd.android.package-archive').then(
                            ()=>{
                         });
                }
            );

    }

便可apache

相關文章
相關標籤/搜索