var wgtVer=null; function plusReady(){ // 獲取本地應用資源版本號 plus.runtime.getProperty(plus.runtime.appid,function(inf){ wgtVer=inf.version; }); } //休眠方法 var ver; function sleep(numberMillis) { var now = new Date(); var exitTime = now.getTime() + numberMillis; while (true) { now = new Date(); if (now.getTime() > exitTime) return; } } mui.plusReady(function(){ plus.runtime.getProperty(plus.runtime.appid, function(inf) { ver = inf.version; var url= severUlr+'version/gainApkVersion'; var client; var ua = navigator.userAgent.toLowerCase(); if(/iphone|ipad|ipod/.test(ua)) { //蘋果手機 $.ajax({ type:"get", dataType:'json', url:"https://itunes.apple.com/lookup?id=111030274",//獲取當前上架APPStore版本信息 data:{ id:111030274 //APP惟一標識ID }, contentType:'application/x-www-form-urlencoded;charset=UTF-8', success:function(data){ console.log("jsjsjsjs"+json2string(data)); $.each(data, function(i,norms) { $.each(norms, function(key,value) { $.each(value, function(j, normItem) { if(j=="version"){ if(normItem>ver){ alert("發現新版本:V"+normItem); document.location.href='https://itunes.apple.com/cn/app/san-gu-hui/id111030274?mt=8'; //上新APPStore下載地址 } } }); }); }); return ; } }); }else if(/android/.test(ua)) { mui.ajax(url,{ data:{ apkVersion: ver, }, dataType:'json', type:'POST', timeout:10000, success:function(data){ if(data.success){ mui.toast("發現新版本:V"+data.data.apkVersion);//獲取遠程數據庫中上新andriod版本號 var dtask = plus.downloader.createDownload(data.data.apkUrl, {}, function(d, status) { if (status == 200) { plus.nativeUI.toast("正在準備環境,請稍後!"); sleep(1000); var path = d.filename;//下載apk plus.runtime.install(path); // 自動安裝apk文件 }else { alert('版本更新失敗:' + status); } }); dtask.start(); }else{ console.log('當前版本號已經是最新'); return; } }, error: function(xhr, type, errerThrown) { mui.toast('網絡異常,請稍候再試'); } }); } }); });