有时候需要给用户一个自主的权利,自主检测app是否是最新版本。


如何实现?
1.点击调用接口,检测是否有更新。
默认APICloud会自动检测版本更新,用户也可以在config.xml里配置autoUpdate为false,然后使用mam模块来检测更新,mam模块还提供自定义事件功能
示例代码:
var mam = api.require('mam');
mam.checkUpdate(function( ret, err ){
if (ret) {
alert( JSON.stringify( ret ) );
} else{
alert( JSON.stringify( err ) );
}
});
返回数据:
{
status:true,
result:
{
update:true,
closed:true,
version:'1.0',
versionDes:'',
closeTip:'',
updateTip:'',
source:'',
time:''
}
}
2.弹出comfirm框,让用户选择是否升级
layer.confirm('有新版本啦!<br/>最新版本:'+ret.result.version+'<br/>更新描述:<br/>'+updateTip+'<br/>发布时间:'+ret.result.time,
{
title:'更新提示',
btn: ['立即更新','取消']
}, function(){
}, function(){
});
3.执行下载操作,api中有download方法
api.download({
url: url,
savePath: 'fs://test.rar',
report: true,
cache: true,
allowResume: true
}, function(ret, err) {
if (ret.state == 1) {
} else {
}
});
4.执行安装installApp方法
api.installApp({
appUri: 'file:
});
api.installApp({
appUri: 'https:
});
整合如下:
function checkUpdate() {
var mam = api.require('mam');
mam.checkUpdate(function( ret, err ){
if (ret) {
if (!ret.status) {
toast('服务器繁忙,请稍后再试');
return;
}
if (ret.result.update) {
var updateTip;
updateTip = ret.result.updateTip.replace(/\r\n/g,"<BR>");
updateTip =updateTip.replace(/\n/g,"<BR>");
layer.confirm('有新版本啦!<br/>最新版本:'+ret.result.version+'<br/>更新描述:<br/>'+updateTip+'<br/>发布时间:'+ret.result.time,
{
title:'更新提示',
btn: ['立即更新','取消']
}, function(){
if (api.systemType == "android") {
api.download({
url : ret.result.source,
report : true
}, function(retdownload, err) {
if (retdownload && 0 == retdownload.state) {
api.toast({
msg : "正在下载应用" + retdownload.percent + "%",
duration : 2000
});
}
if (retdownload && 1 == retdownload.state) {
var savePath = retdownload.savePath;
api.installApp({
appUri : savePath
});
}
});
}
if (api.systemType == "ios") {
api.installApp({
appUri : ret.result.source
});
}
}, function(){
});
return;
} else {
toast('当前已是最新版本');
return;
}
} else{
toast('服务器繁忙,请稍后再试');
return;
}
});
}
本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6484618.html,如需转载请自行联系原作者