uniapp的小程序版本更新检查回调实现可以在小程序onShow或者onLaunch中:
onLaunch触发时间在onShow之前,onShow相对onLaunch对更新实时性要求更高。
onShow: function() {
if(uni.canIUse('getUpdateManager')) {
//检查updateManager的更新
updateManager.onCheckForUpdate(function (res) {
//hasUpdate是否有新版本
if(res.hasUpdate) {
//下载版本完成时的回调
updateManager.onUpdateReady(function() {
uni.showModal({
title: "更新提示",
content: "新版本已更新,是否重启应用?",
success(res) {
if(res.confirm) {
updateManager.applyUpdate();
}
}
});
});
//下载版本失败时的回调
updateManager.onUpdateFailed(function() {
uni.showModal({
title: "新版本已更新!",
content: "新版本已上线,请重启小程序完成更新!"
});
})
}
})
} else {
uni.showModal({
title: "提示",
content: "当前版本无法使用该功能,请更新微信版本!"
});
}
}
uni.canIUse
判断应用的api、回调、参数等是否可在当前版本使用
//首先判断getUpdateManager在当前版本微信是否可用
if(uni.canIUse('getUpdateManager')) {
console.log('当前版本可用');
} else {
uni.showModal({
title: "提示",
content: "当前版本无法使用该功能,请更新微信版本!"
});
}