# UpdateManager

The UpdateManager object, which is used to manage updates. Instances can be obtained via the wx.getUpdateManager API.

# Methods

# UpdateManager.applyUpdate()

Forces a Mini Program to restart and update to the latest version. This API is called after the new Mini Program version is downloaded (i.e., when the onUpdateReady callback is received).

# UpdateManager.onCheckForUpdate(function callback)

Listens on the event that a request for checking for updates is sent to the WeChat backend. WeChat automatically checks for updates when the Mini program cold starts. The developer does not need to trigger this method.

# UpdateManager.onUpdateReady(function callback)

Listens on the event that a newer Mini Program version is available. The app, instead of the developer, triggers the download. A callback is performed after successful download.

# UpdateManager.onUpdateFailed(function callback)

Listens on Mini Program update failure event. The app, instead of the developer, triggers the download when a newer version is available. A callback is performed when the download fails (probably due to network problems).

# Sample Code

const updateManager = wx.getUpdateManager()

updateManager.onCheckForUpdate(function (res) {
  // Callback after request of new version data
  console.log(res.hasUpdate)
})

updateManager.onUpdateReady(function () {
  wx.showModal({
    title: 'Update prompt',
    content: 'The new version is ready. Re-start now?',
    success: function (res) {
      if (res.confirm) {
        // The new version is downloaded. Call applyUpdate to apply the new version and re-start the Mini Program
        updateManager.applyUpdate()
      }
    }
  })
})

updateManager.onUpdateFailed(function () {
  // Failed to download the new version
})

# Tips

  1. In the WeChat DevTools, you can use the "Simulate update at next compilation" switch in "Compilation Mode" for debugging.
  2. Mini Program developer and test versions do not use the "version" concept, so they cannot be used to test version updates.