Update problems
After the new version is released, the old version cannot be automatically updated. When I was developing:
- The dumbest way is to delete the small program and then search and open it again.
Note: To be exact, the earlier the version automatic update method to supplement the more reasonable, especially suitable for the early use of a small number of users to avoid the late use of too many users and the old version still cannot detect the update and timely replacement situation
Implementation scheme
1. Running mechanism of small program
- Hot start: After the small program is opened, it is opened again within a period of time (currently 5 minutes). At this time, the small program in the background is switched to the foreground.
- Cold start: The applet is opened for the first time or re-opened after being destroyed
2. Updated versions
- During cold startup, if a new version is found, the code package of the new version will be downloaded asynchronously, and the local package of the client will be used for startup at the same time, and the small program of the new version will not be applied until the next cold startup. If the latest version is available, use
wx.getUpdateManager API
Process.
Implement a:
/ * *
* Detect the current applets
* Whether the latest version, whether to download, update
* /
function checkUpdateVersion() {
// Create UpdateManager instance
const updateManager = wx.getUpdateManager();
// Check for version updates
updateManager.onCheckForUpdate(function (res) {
// Request a callback for the new version information
if (res.hasUpdate) {
// There is a version update event in the listening applet
updateManager.onUpdateReady(function () {
wx.showModal({
title: "Update Prompt".
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 to apply the new version and restart
updateManager.applyUpdate();
}
},
});
});
updateManager.onUpdateFailed(function () {
// Failed to download the new version
wx.showModal({
title: "There is a new version.".
content:
"Please delete the current small program, go to wechat" Discover - small program "page, search again open yo ~".
});
});
}
});
}
Copy the code
Implementation two: automatic update
In order to unify the version, of course, it is recommended that the small program can be automatically updated, so upgrade the code as follows:
/ * *
* Detect the current applets
* Whether the latest version, whether to download, update
* /
function checkUpdateVersion() {
// Determine whether the wechat version is compatible with the use of apPLETS update mechanism API
if (wx.canIUse("getUpdateManager")) {
// Create UpdateManager instance
const updateManager = wx.getUpdateManager();
// Check for version updates
updateManager.onCheckForUpdate(function (res) {
// Request a callback for the new version information
if (res.hasUpdate) {
// There is a version update event in the listening applet
updateManager.onUpdateReady(function () {
// The new version of TODO has been downloaded, call applyUpdate to apply the new version and restart (automatic update here)
updateManager.applyUpdate();
});
updateManager.onUpdateFailed(function () {
// Failed to download the new version
wx.showModal({
title: "There is a new version.".
content: "Please delete the current small program, go to wechat" Discover - small program "page, search again open oh ~".
});
});
}
});
} else {
// The wechat version of TODO is too low at this time (generally speaking, the version is supported)
wx.showModal({
title: "Warm reminder".
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
The specific use
- In the utils/util. Js
module.exports = {
checkUpdateVersion,
}
Copy the code
2. The introduction of the app. Js