1、安装cordova插件
- 获取版本号: ionic cordova plugin add cordova-plugin-app-version npm install @ionic-native/app-version
- 写入文件:ionic cordova plugin add cordova-plugin-file npm install @ionic-native/file
- 下载文件:ionic cordova plugin add cordova-plugin-file-transfer npm install @ionic-native/file-transfer
- 安装app:ionic cordova plugin add cordova-plugin-file-opener2 npm install @ionic-native/file-opener
2、在app.module.ts中引入上面四个插件,并注入到providers中
import { FileOpener } from '@ionic-native/file-opener/ngx';
import { FileTransfer } from '@ionic-native/file-transfer/ngx';
import { AppVersion } from '@ionic-native/app-version/ngx';
import { File } from '@ionic-native/file/ngx';
3、新建updateService文件
import { FileOpener } from '@ionic-native/file-opener/ngx';
import { FileTransfer, FileTransferObject } from '@ionic-native/file-transfer/ngx';
import { AppVersion } from '@ionic-native/app-version/ngx';
import { File } from '@ionic-native/file/ngx';
private file: File,
private transfer: FileTransfer,
private appVersion: AppVersion,
private fileOpener: FileOpener,
- 首先获取本地版本号和服务器版本号,对比是否需要更新
//获取本地版本号
this.appVersion.getVersionNumber().then((value: any) => {
let localVersion=value
})
//接口获取自身服务器版本号
省略...... 得到 serviceVersion
比较serviceVersion和localVersion的值,判断是否需要更新。
isUpdate(){
//版本号的比较此处就不详细写了
if(serviceVersion>localVersion){
this.showAlert()
}
}
- 若需要更新则提示更新(我的的alert的方式,也可以用modal等其他方式。这个可以随意)
async showAlert() {
let appVersionInfo = this.punlicService.getLocalstorageValue(this.config.cache.appVersionInfo);//获取更新信息
//弹窗提示更新
let forceButtons = [
{text: '取消',role: 'cancel',cssClass: 'secondary',handler: (blah) => {}},
{text: '确认', handler: () => {this.downloadApp();}}
];let buttons = [ { text: '确认',handler: () => { this.downloadApp();}}];
let title = appVersionInfo.updateMode == 0 ? '发现新版本,立即更新' : '发现新版本,是否立即升级?';
const alert = await this.alertController.create({
header: title,
message: appVersionInfo.updateLog,
backdropDismiss: false,
cssClass: 'update-app',
mode: 'ios',
buttons: appVersionInfo.updateMode == 0 ? buttons : forceButtons
});
await alert.present()
}
-
下载apk
uploadProgressNum = 0;//下载进度值downloadApp() {
const targetUrl = 'http://****.com/apk/appName.apk';//apk的服务器下载地址
const fileTransfer: FileTransferObject = this.transfer.create();//app的安装目录
fileTransfer.download(targetUrl, this.file.dataDirectory + 'appName.apk').then((entry) => {
//安装并打开APPthis.fileOpener.open(entry.toURL(),
'application/vnd.android.package-archive')
.then(() => {
console.log('app已打开')
})
.catch(e => {
console.log('打开app失败')
});
}, (error) => {
alert(‘安装失败‘);
});
this.showProgressnum();//展示下载进度
let uploadProgressNumDom = null;
//获取下载进度fileTransfer.onProgress((event) => {
if (!uploadProgressNumDom ) {
uploadProgressNumDom = document.getElementById('update-progress');
}
uploadProgressNumDom .innerHTML = '下载进度:0%';
this.uploadProgressNum = Math.ceil(event.loaded / event.total * 100);
if (this.uploadProgressNum == 100) {
uploadProgressNumDom .innerHTML = '下载完成';
} else {
console.log(this.uploadProgressNum + '%')
uploadProgressNumDom .innerHTML = '下载进度:' + this.uploadProgressNum + '%';
}
});
}
-
下载进度条展示
async showProgressnum() {
const alert = await this.alertController.create({
header: null,
message: '<div id="update-progress></div>',
backdropDismiss: false,
cssClass: '',
mode: 'ios',
buttons: []
});
await alert.present();
let timer: any;
timer = setInterval(() => {
if (this.uploadProgressNum== 100) {
alert.dismiss();
clearInterval(timer);
}
}, 1000)
}
- 在你需要提示更新的地方引入updateService,调用isUpdate()就好了。
- 另外可能会遇到下载之后,不覆盖安装的情况,这种情况可以在platforms/android/app/src/main/AndroidManifest.xml中添加一行代码
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
这样就可以了。
这种方式是前台更新的方式,还有一种后台更新,暂时还没有研究。