uniapp
App.vue
onShow: function () {
// #ifndef H5
const updateManager = uni.getUpdateManager();
updateManager.onCheckForUpdate(function (res) {
if (res.hasUpdate) {
updateManager.onUpdateReady(function (res) {
uni.showModal({
title: "更新提示",
content: "新版本已经准备好,是否重启应用?",
success(res) {
if (res.confirm) {
updateManager.applyUpdate();
}
},
});
});
}
});
// #endif
},
这里主要是用了uniapp提供的两个api ,getUpdateManager和applyUpdate进行更新判断并且重启更新。注意,因为很多时候,uniapp会打包成h5和小程序,所以为了防止异常,需要加入条件编译语法,只有在非h5的平台才执行小程序强制更新的操作。
微信小程序
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function (res) {
if(!res.hasUpdate) return
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success: function (res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
}
}
})
})
}