当前位置:网站首页>[untitled]
[untitled]
2022-04-23 02:14:00 【Chang'an CC】
How to prompt the user to force update after the wechat applet releases a new version !
Premise
After the applet is updated , If you do not actively delete the applet, then enter , So the small program still uses the old version code , Unable to update the latest version . In this way, if there is an urgent problem, repair it online , Users can't force updates at the first time .
terms of settlement
The official provided UpdateManager Update manager object , Used to detect whether a new version has been released .
UpdateManager Object provides the following four methods :
UpdateManager.applyUpdate() Force the applet to restart and use the new version . After downloading the new version of the applet ( I will receive
onUpdateReady Callback ) call .UpdateManager.onCheckForUpdate(function callback)
Listen for the event of requesting wechat background to check and update the result . Wechat automatically checks for updates when the applet is cold started , There is no need for developers to actively trigger .UpdateManager.onUpdateReady(function callback)
The listener applet has a version update event . The client actively triggers the download ( No developer trigger required ), Callback after successful downloadUpdateManager.onUpdateFailed(function callback)
Listen for applet update failure events . There is a new version of the applet , The client actively triggers the download ( No developer trigger required ), Download failed ( It may be due to the network, etc ) Back to
Official sample code :
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function (res) {
// Call back after requesting new version information
console.log(res.hasUpdate)
})
updateManager.onUpdateReady(function () {
wx.showModal({
title: ' Update hints ',
content: ' The new version is ready , Restart application or not ?',
success: function (res) {
if (res.confirm) {
// The new version has been downloaded , call applyUpdate Apply new version and restart
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(function () {
// New version download failed
})
Official demo in , Only the most basic update tips , No exception handling . And the official also said that this function is from the basic library 1.9.90 Start supporting , Compatibility is required for the lower version .
in addition , If there are major adjustments in the current version update , Users must update , There are two situations :
-
The first method is to give a prompt in the callback when the user clicks cancel , And re enter the version prompt process .
-
The second way is to remove the Cancel button , Only the OK button . Force users to upgrade , To continue to use .
The improved code is as follows :
/** * Detect current applet * Is it the latest version , Need to download 、 to update */
function checkUpdateVersion() {
// Determine whether the wechat version Compatible with applet update mechanism API Use
if (wx.canIUse('getUpdateManager')) {
const updateManager = wx.getUpdateManager();
// Check for version updates
updateManager.onCheckForUpdate(function (res) {
if (res.hasUpdate) {
updateManager.onUpdateReady(function () {
wx.showModal({
title: ' reminder ',
content: ' New version detected , Do you want to restart the applet ?',
showCancel: false,
success: function (res) {
if (res.confirm) {
// The new version has been downloaded , call applyUpdate Apply new version and restart
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(function () {
// New version download failed
wx.showModal({
title: ' There is a new version ',
content: ' Please delete the applet , Re search into ',
})
})
}
})
} else {
wx.showModal({
title: ' Warm tips ',
content: ' Current wechat version is too low , This feature is not available , Please upgrade to the latest wechat version and try again .'
})
}
}
stay app.js Of onLaunch Add the check method to the method
// Version automatically updates the code
this.checkUpdateVersion()
How to test ?
Be careful :
Wechat developers can use 「 Compile mode 」 Under the 「 Next time you compile a simulation update 」 Switch to debug
Small program development version / The experience version doesn't have 「 edition 」 Concept , So it can't be in the development version / Test the updated version on the experience version
版权声明
本文为[Chang'an CC]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230212118052.html
边栏推荐
- 世界读书日 | 技术人不要错过的好书(IT前沿技术)
- Log4j2 configuration
- 013_ Analysis of SMS verification code login process based on session
- 中金财富跟中金公司是一家公司吗,安全吗
- LeetCode 349. Intersection of two arrays (simple, array) Day12
- On LAN
- New book recommendation - IPv6 technology and application (Ruijie version)
- 假如404页面是这样的 | 每日趣闻
- JDBC cannot connect to MySQL, and the error is access denied for user 'root' @ '* * *' (using password: Yes)
- Consider defining a bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs‘
猜你喜欢
Network jitter tool clumsy
简洁开源的一款导航网站源码
Find the largest number of two-dimensional arrays
001_redis设置存活时间
Thinkphp内核开发盲盒商城源码v2.0 对接易支付/阿里云短信/七牛云存储
009_Redis_RedisTemplate入门
006_ redis_ Sortedset type
009_ Redis_ Getting started with redistemplate
Esp32 message queue using FreeRTOS
Introduction to esp32 Bluetooth controller API
随机推荐
hyperscan --- 1
What categories do you need to know before using proxy IP?
Usage of vector common interface
How to configure iptables to realize local port forwarding
World Book Day 𞓜 a good book that technicians should not miss (it cutting-edge technology)
Chinese scientists reveal a new mechanism for breaking through the bottleneck of rice yield
App optimization and advanced scoreboard Part 2 [Mui + flask + mongodb]
89 régression logistique prédiction de la réponse de l'utilisateur à l'image de l'utilisateur
Log4j2 configuration
PTA: Romantic reflection [binary tree reconstruction] [depth first traversal]
世界读书日 | 技术人不要错过的好书(IT前沿技术)
Summary of I / O knowledge points
用TensorFlow实现线性回归(包括过程中出现的问题及解决方法)
从0开始开发一个chrome插件(2)
openstack 服务的启动
Is the sinking coffee industry a false prosperity or the eve of a broken situation?
电源电路设计原来是这么回事
从开源爱好者到 Apache 董事,一共分几步?
Wechat public platform test number application, authorized login function and single sign on using hbuilder X and wechat developer tools
New book recommendation - IPv6 technology and application (Ruijie version)