As shown in the figure, when the new version of the small program is released, if the user has previously accessed the small program and entered through the opened small program (not manually deleted), the prompt will pop up to remind the user to update the new version. Users can click OK to automatically restart the update, click Cancel to close the popover, no longer update.
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(res) {if (res.confirm) {// The new version has been downloaded, 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 has also been officially stated that this feature base library 1.9.90 will be supported, but the lower version will need to be compatible, so the side code will need to be improved.
On the other hand, if there is a major change in the current version update that must be updated by the user, you can prompt it in the callback that the user clicks to cancel and re-enter the version prompt process. As shown in the picture below, in the above update prompt, the user clicks cancel, the following prompt popup pops up; the user clicks OK, the version will be updated; click cancel, the above update prompt will be called again. In short, users can access the applet normally only after updating it (if not necessary, it is recommended to use with caution).
//app.js App({ onLaunch: function(options) { this.autoUpdate() }, AutoUpdate :function(){console.log(new Date()) var self=this if (wx.caniuse ('getUpdateManager')) {const updateManager = wx.getUpdateManager() //1. . Check the small program for the new version released updateManager onCheckForUpdate (function (res) {/ / request callback if the new version information (res) hasUpdate) {/ / 2. Small program is the new version, download the new version is silent, prepare for update updateManager. OnUpdateReady (function () {the console. The log (new Date ()) wx. ShowModal ({title: 'Update hint ', Content:' The new version is ready. Do you want to restart the application? ', success: function (res) { if (res.confirm) { //3. Download the new version of good, call applyUpdate application. The new version and restart updateManager applyUpdate ()} else if (res) cancel) {/ / forced updates if required, are given second popup window, if you don't need, Wx. showModal({title: 'warm hints ~', content:' This version update involves the addition of new functions, the old version can not be accessed normally oh ~', success: function (res) { self.autoUpdate() return; If (res.confirm) {// The new version has been downloaded, Call applyUpdate use new version and restart updateManager. ApplyUpdate ()} else if (res) cancel) {/ / back to the version update tip self autoUpdate ()}}}}}) })}) updateManager. OnUpdateFailed (function () {/ / new version download failure wx. ShowModal ({title: 'it has been a new version yo ~', the content: })})})} else {// If you want users to experience your applet on the latest version of the client, you can prompt wx.showmodal ({title: 'Warning ', content:' The current wechat version is too early to use this function, please upgrade to the latest wechat version and try again. '})}}})Copy the code
Updated version of the simulation test:
-
Wechat developer tools can be debugged through the “next compilation Simulation update” switch under “Compile Mode”;
-
Applets do not have the concept of “version”, so it is not possible to test version updates on the development version/experience version;
For developer tools, you can verify tests like this:
Click the compilation mode Settings drop-down list, and then click “Add compilation mode”, in the custom compilation conditions popup interface, click simulate update next time compilation, and then click OK, recompilation is OK.
It is important to note that this mode will not work after the simulation is updated once, and you will need to reset this compilation mode for subsequent tests.
Is there a delay in updating prompts?
Test on the developer tools, update tip pop-up window in a small program interface load out after five or six seconds to play, this is due to the small program after detected a new version, called UpdateManager. OnUpdateReady (function callback) version update monitor, At this point, the client automatically triggers the download (no need for the developer to trigger the download), and the download is successfully triggered. That is to say, the update pop-up we mentioned above popped up after the applet had detected the new version and finished downloading the new version, hence the difference of a few seconds. In this case, it is necessary to improve again, at least should be in the small program compilation detection of a new version should be given the update prompt, as for the preparation of the new version download, can be in the user click the confirm button after the code modification is as follows:
App({ onLaunch: function(options) { this.autoUpdate() }, autoUpdate: If (wx.caniUse ('getUpdateManager')) {const updateManager = wx.getUpdateManager() //1. . Check the small program for the new version released updateManager onCheckForUpdate (function (res) {/ / request callback if the new version information (res) hasUpdate) {/ / detected a new version, you need to update, Give a prompt wx.showModal({title: 'Update prompt ', Content:' New version detected, should you download the new version and restart the applet? ', success: function(res) { if (res.confirm) { //2. Self. downLoadAndUpdate(updateManager)} else if (res.cancel) {// The user clicks the cancel button. If the update is forced, a secondary popup window is displayed. Wx. showModal({title: 'warm tip ~', content: ShowCancel :false,// Hide cancel button confirmText:" confirmText ",// only keep confirmText button SUCCESS: Function (res) {if (res.confirm) { DownLoadAndUpdate (updateManager)}}})}}})}})}} else {// If you want users to experience your applet on the latest version of the client, Wx. showModal({title: 'prompt ', content:' The current wechat version is too low to use this function, please upgrade to the latest wechat version and try again. */ downLoadAndUpdate: function (updateManager){var self=this wx.showloading (); / / silence. Download a small program that update new version updateManager onUpdateReady (function () {wx. HideLoading () / / new version has been downloaded, Call applyUpdate use new version and restart updateManager. ApplyUpdate ()}) updateManager. OnUpdateFailed (function () {/ / new version download failed Wx. showModal({title: 'new version is available ~', content:' new version is available ~, please delete the current small program and search again ~',})})Copy the code
As above, after detecting a new version of the small program, a pop-up window will be presented to prompt the user to download the new version and restart the small program. After the user clicks the “OK” button, the new version of the small program will be downloaded and updated. Also for the convenience of call, the new version of the download and small program to restart the application separately packaged.
Thus, loading from the widget to the popup version update popup only takes the time it takes to call the new version detection API and return the results (2-3 seconds for developer tools testing), which is still a bit of a delay, but for now.
Conclusion:
In summary, in any case, it takes a certain amount of time to use the applets version update detection function (one or two seconds is not a short time). If the user performs an operation within one or two seconds of detection, the update prompt pop-up window will interrupt the user’s operation. After all, it is not frequently updated, so this aspect is acceptable.
In addition, loading is recommended when downloading a new version of the package, so that users know it is downloading and then automatically restart after downloading. In this way, the whole process is much smoother.
I have sorted out and completed a mind map, which can assist in understanding:
Other notes:
Base library minimum version Settings:
If you don’t want to make API support judgments, you can set minimal version support for applets.
Background – Settings – open the small program management library minimum version Settings, according to the existing small program on visit or small official data, set up the basis of a more popular library version, so you can into the minimize API compatibility judgment, also can cause user to update WeChat version, to support the normal operation of small programs, Experience some advanced features of applets.
Original address: www.cnblogs.com/xyyt