微信上强制更新小程序
发布于 5 年前 作者 jing37 7371 次浏览 来自 问答

我们已经发布了多次新版本,但是以前没有添加更新的代码,新版本添加了小程序更新的代码。但是以前打开过的用户平时用旧版本,更新新版本是麻烦。如果小程序管理界面添加更新功能,用户真容易更新新版本。

2 回复
updateApp:function(){
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      if (res.hasUpdate) {
        wx.showLoading({
          title: '更新下载中...',
        })
      }
    })
    updateManager.onUpdateReady(function () {
      wx.hideLoading();
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好,是否重启应用?',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
 
    })
    updateManager.onUpdateFailed(function () {
      // 新的版本下载失败
      wx.hideLoading();
      wx.showToast({ title: '下载失败...', icon: "none" });
    })
  },

app.js

onLaunch() 调用一下就好了

这个强制更新,在开发阶段能模拟测试一下吗?

好像只能在正式版上线 ,版本号发生变化的时候,才会触发这个 ?

回到顶部