UpdateManager.onCheckForUpdate 监听函数

微信小程序插件:不支持

功能描述

监听向微信后台请求检查更新结果事件。微信在微信小程序每次启动(包括热启动)时自动检查更新,不需由开发者主动触发。

参数

function listener

向微信后台请求检查更新结果事件的监听函数

参数

Object res
属性 类型 说明
hasUpdate boolean 是否有新版本

示例代码

示例代码

UpdateManager

UpdateManager 对象,用来管理更新,可通过 wx.getUpdateManager 接口获取实例。

方法

UpdateManager.applyUpdate()

强制微信小程序重启并使用新版本。在微信小程序新版本下载完成后(即收到 onUpdateReady 回调)调用。

UpdateManager.onCheckForUpdate(function listener)

监听向微信后台请求检查更新结果事件。微信在微信小程序每次启动(包括热启动)时自动检查更新,不需由开发者主动触发。

UpdateManager.onUpdateReady(function listener)

监听微信小程序有版本更新事件。客户端主动触发下载(无需开发者触发),下载成功后回调

UpdateManager.onUpdateFailed(function listener)

监听微信小程序更新失败事件。微信小程序有新版本,客户端主动触发下载(无需开发者触发),下载失败(可能是网络原因等)后回调

示例代码

const updateManager = wx.getUpdateManager()

updateManager.onCheckForUpdate(function (res) {
  // 请求完新版本信息的回调
  console.log(res.hasUpdate)
})

updateManager.onUpdateReady(function () {
  wx.showModal({
    title: '更新提示',
    content: '新版本已经准备好,是否重启应用?',
    success: function (res) {
      if (res.confirm) {
        // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
        updateManager.applyUpdate()
      }
    }
  })
})

updateManager.onUpdateFailed(function () {
  // 新版本下载失败
})

Tips

  1. 微信开发者工具上可以通过「编译模式」下的「下次编译模拟更新」开关来调试
  2. 微信小程序开发版/体验版没有「版本」概念,所以无法在开发版/体验版上测试更版本更新情况

PluginUpdateManager.onCheckForUpdate 监听函数

微信小程序插件:不支持

功能描述

监听向微信后台请求检查插件更新结果事件。微信在微信小程序每次启动(包括热启动)时自动检查插件更新,不需由开发者主动触发。

参数

function listener

向微信后台请求检查插件更新结果事件的监听函数

参数

Object res
属性 类型 说明
hasUpdate boolean 是否有插件新版本
pluginVersion string 插件新版本号。仅当 hasUpdatetrue 时返回

PluginUpdateManager

基础库 2.25.0 开始支持,低版本需做兼容处理。

PluginUpdateManager 对象,用来管理插件更新,可通过 wx.getPluginUpdateManager 接口获取实例。

方法

PluginUpdateManager.onCheckForUpdate(function listener)

监听向微信后台请求检查插件更新结果事件。微信在微信小程序每次启动(包括热启动)时自动检查插件更新,不需由开发者主动触发。

PluginUpdateManager.onUpdateReady(function listener)

监听插件有版本更新事件。客户端主动触发下载(无需开发者触发),下载成功后回调

PluginUpdateManager.onUpdateFailed(function listener)

监听插件更新失败事件。插件有新版本,客户端主动触发下载(无需开发者触发),下载失败(可能是网络原因等)后回调

示例代码

const pluginUpdateManager = wx.getPluginUpdateManager({
  pluginId: 'wx1234567890abcdef'
})

pluginUpdateManager.onCheckForUpdate(function (res) {
  // 请求完插件新版本信息的回调
  console.log(res.hasUpdate)
  if (res.hasUpdate) {
    console.log(res.pluginVersion)
  }
})

pluginUpdateManager.onUpdateReady(function (res) {
  wx.showModal({
    title: '更新提示',
    content: '插件新版本已经准备好,是否重启应用?',
    success: function (modalRes) {
      if (modalRes.confirm) {
        // 插件新版本已经下载好,调用 applyUpdate 应用新版本并重启
        wx.getUpdateManager().applyUpdate()
      }
    }
  })
})

pluginUpdateManager.onUpdateFailed(function (res) {
  // 插件新版本下载失败
  console.log(res.pluginVersion)
})