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

相关文章
相关标签/搜索