Hbuilder,自动升级功能实现
2.制作APP资源升级包
步骤:打开Hbuilder菜单-》发行-》制作APP资源升级包-》选择保存路径,点击“确定”保存wgt文件
最后将生成的.wgt文件放到服务器上app-update文件夹中。
注意:每次更新app都需要将最新升级包更新到服务器。
3.升级代码如下:(下面这段代码会自动判断,当与当前版本不等时,就会升级)
var wgtVer = null;
	function plusReady(){
		// 获取本地应用资源版本号
		plus.runtime.getProperty(plus.runtime.appid,function(inf){
			wgtVer = inf.version;
			version.innerText = (wgtVer);
			console.log("当前应用版本:"+wgtVer);
		});
	}
	if(window.plus){
		plusReady();
	}else{
		document.addEventListener('plusready',plusReady,false);
	};
	
	// 检测更新
	var checkUrl = "http://app.telecomlab.cn/app-update/version.php";
	function checkUpdate(){
		plus.nativeUI.showWaiting("检测更新");
		var xhr = new XMLHttpRequest();
		xhr.onreadystatechange = function(){
			switch(xhr.readyState){
				case 4:
				plus.nativeUI.closeWaiting();
				if(xhr.status == 200){
					console.log("检测更新成功!:"+xhr.responseText);
					var newVer = xhr.responseText;
					if(wgtVer&&newVer&&(wgtVer != newVer)){
						// 弹出系统确认对话框
						plus.nativeUI.confirm( "确定下载更新?", function(e){
							if(e.index == 0){
								downWgt();	// 下载wgt资源包
							}else{}
						}, "下载可用更新", ["下载","取消"] );
					}else{
						plus.nativeUI.toast("无可用更新!");
					}
				}else{
					console.log("检测更新失败!");
					plus.nativeUI.toast("检测更新失败!");
				}
				break;
				default:
				break;
			}
		}
		xhr.open('GET',checkUrl);
		xhr.send();
	};
	
	// 下载wgt文件
	var wgtUrl = "http://app.telecomlab.cn/app-update/update.wgt";
	function downWgt(){
		plus.nativeUI.showWaiting("下载更新");
		plus.downloader.createDownload( wgtUrl, {filename:"_doc/update/"}, function(d,status){
			if ( status == 200 ) { 
				console.log("下载更新成功:"+d.filename);
				installWgt(d.filename);	// 安装wgt资源包
			} else {
				console.log("下载更新失败!");
				plus.nativeUI.toast("下载更新失败!");
			}
			plus.nativeUI.closeWaiting();
		}).start();
	};
	
	// 更新应用资源
	function installWgt(path){
		plus.nativeUI.showWaiting("安装更新");
		plus.runtime.install(path,{},function(){
			plus.nativeUI.closeWaiting();
			console.log("安装更新成功!");
			plus.nativeUI.alert("更新完成!",function(){
				plus.runtime.restart();
			});
		},function(e){
			plus.nativeUI.closeWaiting();
			console.log("安装更新失败!["+e.code+"]:"+e.message);
			plus.nativeUI.toast("安装更新失败!");
		});
	};
	
上一篇:Git版本撤销
下一篇: iphone网页输入框隐藏的解决方法
 
			