HBuilder的熱更新
阿新 • • 發佈:2019-02-07
HBuilder 的熱更新
導讀:我在移動APP開發之中特別困擾appStore的更新問題,沒次改變點東西都需要重新發包進行稽核,之前還不知道HBuilder有這個熱更新的功能,後來聽同事說可以熱更新,不用每次都上傳蘋果商店進行稽核,得到這個訊息我很興奮,於是在網上查閱了資料,自己變行動起來,一下是我的總結。 1、首先我們要獲取當前安裝包的version,將獲取到當前APP安裝的版本與伺服器上的版本進行比較,如果存在最新版本可以提示使用者進行下載更新。var version; // 獲取本地應用資源版本號 plus.runtime.getProperty(plus.runtime.appid,function(inf){ //獲取當前版本號 version=inf.version;}
2、如果使用者選擇更新可以使用一下方法進行更新。
// 下載wgt檔案 function downWgt(){ //plus.nativeUI.showWaiting("下載wgt檔案..."); plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){ if ( status == 200 ) { console.log("下載wgt成功:"+d.filename); installWgt(d.filename); // 安裝wgt包 } else { console.log("下載wgt失敗!"); plus.nativeUI.alert("下載wgt失敗!"); } plus.nativeUI.closeWaiting(); }).start(); }
// 更新應用資源 function installWgt(path){ //plus.nativeUI.showWaiting("安裝wgt檔案..."); plus.runtime.install(path,{},function(){ plus.nativeUI.closeWaiting(); console.log("安裝wgt檔案成功!"); plus.nativeUI.alert("應用資源更新完成!",function(){ plus.runtime.restart(); }); },function(e){ plus.nativeUI.closeWaiting(); console.log("安裝wgt檔案失敗["+e.code+"]:"+e.message); plus.nativeUI.alert("安裝wgt檔案失敗["+e.code+"]:"+e.message); }); };
3、上面提到的下載WGT檔案是由HBuilder生成的。先填寫版本號等資訊在進行WGT檔案的生成。
然後HBuilder的選單欄中選擇“發行”--》“製作移動app資源升級包”,將生成好的WGT檔案放到伺服器即可。