整包更新
與熱更新不同,整包更新比對的版本號是應用版本名稱
//檢測當前版本號
dqbanben:function(){
var that=this;
//在頁面中初始化plus插件
mui.init();
mui.plusReady(function(){
// 獲取本地應用資源版本號
that.wgtVer= plus.runtime.version;
that.checkUpdate();
});
},
//檢查更新
checkUpdate:function(){
var that=this;
this.$http.post(this.GLOBAL.host+'/version/checkUpdate', {
version:that.wgtVer
})
.then(function (res) {
if(res.data.code==0){
if(res.data.result==1){//跟後臺傳過來的版本號比對,如果版本號不一致
if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) { //如果是蘋果手機
plus.nativeUI.confirm("檢測到有新版本,是否更新",function(e){
if(e.index==0){ //如果選擇更新
window.location.href="itms-services://?action=download-manifest&url=https://ios.17rua.top/static/ios/x5.plist";
//不經過蘋果商店下載(不懂得看我的另一篇文章)
plus.nativeUI.showWaiting("正在下載...");
}
},"",["立即更新","以後再說"]);
return;
}else{
plus.nativeUI.confirm("檢測到有新版本,是否更新",function(e){
if(e.index==0){
that.downWgt(res.data.url);//下載文件
}else{
plus.runtime.quit();//安卓控制不更新退出應用
}
},"",["立即更新","以後再說"]);
}
sessionStorage.setItem('kbj_banben',true);//檢驗過一次版本就加入緩存,不在檢測
}else{
sessionStorage.setItem('kbj_banben',true);
return;
}
}else{
alert("獲取數據失敗")
}
})
.catch(function (error) {
alert("請檢查網絡連接")
});
},
//下載資源包
downWgt:function(wgtUrl){
var that=this;
var task=plus.downloader.createDownload( wgtUrl, {}, function(download,status){ //安裝到手機的目錄
if ( status == 200 ) {
plus.runtime.install(download.filename); // 安裝下載的apk文件
} else {
mui.toast("下載更新失敗!");
plus.nativeUI.closeWaiting();
}
});
//監聽下載
task.addEventListener("statechanged", function (download, status) {
switch (download.state) {
case 2:
plus.nativeUI.showWaiting("正在下載...");
break;
case 3:
//進度條百分比 totalSize爲總量,baifen爲當前下載的百分比
if(that.totalSize==0){
that.totalSize=parseInt(download.totalSize);
}
if(parseInt(download.downloadedSize/that.totalSize*100)!=that.baifen){
that.baifen=parseInt(download.downloadedSize/that.totalSize*100);
}
break;
case 4:
mui.toast("下載完成");
plus.nativeUI.closeWaiting();
break;
}
});
task.start();
},
這樣就可以實現ios與安卓的整包更新,而且IOS不用經過應用商店更新。