1、所需要的插件: 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 //文件打开系统
ionic-native需要升级到1.3.20以上;
2、ts文件调用 2.1版本号读取:
import {AppVersion} from "ionic-native/dist/index";
getVerNumber(){
AppVersion.getVersionNumber().then((version)=> {
this.versionNo = version;
});
}
2.2 版本下载和版本打开
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(
()=>{
});
}
);
}
即可