当前位置:网站首页>【无标题】
【无标题】
2022-04-23 02:12:00 【常安cc】
如何在微信小程序发布新版本后提示用户强制更新!
前提
小程序在更新版本后,如果没有主动删除小程序再进入,那么小程序还是用的旧版本代码,无法更新最新版本。这样如果有紧急问题修复上线,用户却不能第一时间强制更新。
解决办法
官方提供了 UpdateManager 更新管理器对象,用来检测是否有新的版本发布。
UpdateManager 对象提供以下四种方法:
UpdateManager.applyUpdate() 强制小程序重启并使用新版本。在小程序新版本下载完成后(即收到
onUpdateReady 回调)调用。UpdateManager.onCheckForUpdate(function callback)
监听向微信后台请求检查更新结果事件。微信在小程序冷启动时自动检查更新,不需由开发者主动触发。UpdateManager.onUpdateReady(function callback)
监听小程序有版本更新事件。客户端主动触发下载(无需开发者触发),下载成功后回调UpdateManager.onUpdateFailed(function callback)
监听小程序更新失败事件。小程序有新版本,客户端主动触发下载(无需开发者触发),下载失败(可能是网络原因等)后回调
官方示例代码:
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function (res) {
// 请求完新版本信息的回调
console.log(res.hasUpdate)
})
updateManager.onUpdateReady(function () {
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success: function (res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(function () {
// 新版本下载失败
})
官方提供的demo中,只有最基本的更新提示,并未做异常处理。而且官方也说了这个功能从基础库 1.9.90 开始支持,低版本需做兼容处理。
另外,如果当前版本更新有重大调整,一定需要用户更新,有两种情况:
-
第一种方法是可以在用户点击取消的回调中给出提示,并重新进入版本提示流程。
-
第二种方法就是去除取消按钮,只有确定按钮。强制用户必须升级,才能继续使用。
改进后代码如下:
/** * 检测当前的小程序 * 是否是最新版本,是否需要下载、更新 */
function checkUpdateVersion() {
//判断微信版本是否 兼容小程序更新机制API的使用
if (wx.canIUse('getUpdateManager')) {
const updateManager = wx.getUpdateManager();
//检测版本更新
updateManager.onCheckForUpdate(function (res) {
if (res.hasUpdate) {
updateManager.onUpdateReady(function () {
wx.showModal({
title: '温馨提示',
content: '检测到新版本,是否重启小程序?',
showCancel: false,
success: function (res) {
if (res.confirm) {
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(function () {
// 新版本下载失败
wx.showModal({
title: '已有新版本',
content: '请您删除小程序,重新搜索进入',
})
})
}
})
} else {
wx.showModal({
title: '溫馨提示',
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
})
}
}
在 app.js 的 onLaunch 方法中增加引入检查方法
// 版本自动更新代码
this.checkUpdateVersion()
如何测试?
注意:
微信开发者工具上可以通过「编译模式」下的「下次编译模拟更新」开关来调试
小程序开发版/体验版没有「版本」概念,所以无法在开发版/体验版上测试更版本更新情况
版权声明
本文为[常安cc]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_55546349/article/details/124348520
边栏推荐
- Tp6 Alibaba cloud SMS window reports curl error 60: SSL certificate problem: unable to get local issuer certificate
- Today will finally write system out. Println()
- Gray scale range corresponding to colors (red, yellow, green, blue, purple, pink, brick red and magenta) in HSV color space
- Makefile文件是什麼?
- What problems will you encounter when dialing VPS?
- 006_redis_jedis快速入门
- Lane cross domain problem
- What are the common proxy IP problems?
- leetcode:27. 移除元素【count remove小操作】
- 用TensorFlow实现线性回归(包括过程中出现的问题及解决方法)
猜你喜欢
010_StringRedisTemplate
Kubernetes cluster installation based on Kirin SP10 server version
002_ Redis_ Common operation commands of string type
Dynamic batch processing and static batch processing of unity
arduino esp8266 网络升级 OTA
New book recommendation - IPv6 technology and application (Ruijie version)
001_ Redis set survival time
Gray scale range corresponding to colors (red, yellow, green, blue, purple, pink, brick red and magenta) in HSV color space
每日一题(2022-04-22)——旋转函数
Is it better to use a physical machine or a virtual machine to build a website?
随机推荐
Kubernetes cluster installation based on Kirin SP10 server version
Use of push() and pop()
arduino esp8266 网络升级 OTA
What problems will you encounter when dialing VPS?
Chinese scientists reveal a new mechanism for breaking through the bottleneck of rice yield
tp6阿裏雲短信 window 報 cURL error 60: SSL certificate problem: unable to get local issuer certificate
Use Xdebug breakpoint debugging in postman
Leetcode39 combined sum
What business scenarios will the BGP server be used in?
How to call out services in idea and display the startup class in services
What should I pay attention to when using proxy IP?
Lane cross domain problem
Unity editor hierarchy drop-down menu extension
006_ redis_ Jedis quick start
What is an API interface?
010_ StringRedisTemplate
想体验HomeKit智能家居?不如来看看这款智能生态
Shardingsphere broadcast table and binding table
011_RedisTemplate操作Hash
从开源爱好者到 Apache 董事,一共分几步?