This is the first day of my August challenge

The premise

Small program in the updated version, if you do not take the initiative to delete the small program to enter, then the small program is still using the old version of the code, can not update the latest version. So if there is an emergency problem fix online, users can not be the first time to force the update.

The solution

The UpdateManager UpdateManager object is provided to detect whether a new version has been released.

The UpdateManager object provides the following four methods:

UpdateManager. ApplyUpdate () forced restart small program and use the new version. Called after the new version of the applet is downloaded (that is, the onUpdateReady callback is received).

UpdateManager. OnCheckForUpdate function (the callback) to monitor the update request WeChat background check result events. Wechat will automatically check for updates when the small program starts cold, without being triggered by the developer.

UpdateManager. OnUpdateReady function (the callback) to monitor small program version update events. The client automatically triggers the download (without triggering by the developer), and the download is successfully triggered

UpdateManager. OnUpdateFailed function (the callback) to monitor small program update failed events. There is a new version of the small program. The client automatically triggers the download (no need to be triggered by the developer), and the download fails (possibly due to network reasons)

Official sample code:

Const updateManager = wx. GetUpdateManager () updateManager. OnCheckForUpdate (function (res) {/ / callback request through the new version information The console. The log (res) hasUpdate)}) updateManager. OnUpdateReady (function () {wx. ShowModal ({title: 'updates, content: 'The new version is ready. Do you want to restart the application? ', success: Function (res) {if (res.confirm) { Call applyUpdate use new version and restart updateManager. ApplyUpdate ()}}})}) updateManager. OnUpdateFailed (function () {/ / download the new version failed})Copy the code

In the official demo, only the most basic update prompt is provided without exception handling. It is also stated that this feature will be supported from base library 1.9.90 onwards, with compatibility processing required for earlier versions.

In addition, if there is a major change in the current version update, the user must update, there are two cases:

The first is that you can prompt a callback that the user clicks to cancel and re-enter the version prompt process.

The second way is to remove the cancel button and just have the OK button. Force the user to upgrade to continue using.

The improved code is as follows:

/** * Check whether the current applets * is the latest version, */ function checkUpdateVersion() {if (wx.caniUse ('getUpdateManager')) {const updateManager = wx.getUpdateManager(); / / test version update updateManager. OnCheckForUpdate (function (res) {if (res) hasUpdate) {updateManager. OnUpdateReady (function () { Wx. showModal({title: 'warm tip ', Content:' New version detected, do you want to restart the applet? ', showCancel: false, success: Function (res) {if (res.confirm) { Call applyUpdate use new version and restart updateManager. ApplyUpdate ()}}})}) updateManager. OnUpdateFailed (function () {/ / download the new version fails Wx.showmodal ({title: 'new version ', content:' Please delete applet and search again ',})})}})} else {wx.showmodal ({title: 'warm tips ', content: 'The current wechat version is too early to use this function. Please upgrade to the latest wechat version and try again. ' }) } } module.exports = { checkUpdateVersion }Copy the code

Add import check method to app.js onLaunch method

Check.checkupdateversion ()Copy the code

How to test?

Note:

Wechat developer tools can be debugged through the “next compilation Simulation update” switch under “Compilation mode”

Applets do not have the concept of “version”, so you cannot test the version updates on the development/experience version

I am a strange uncle selling nuts – a Buddha department front-end development, will throw a throw photography, like to toss, love food. Share front-end tips, notes and a variety of interesting APP and resource tutorials. ♥