# UpdateManager.triggerCheckUpdate

主动触发更新小程序。开发者调用该方法时,首先触发onCheckForUpdate事件;当前运行版本低于线上版本时,会强制更新小程序,若下载成功或本地已存在线上最新包,则会触发onUpdateReady事件,否则触发onUpdateFailed事件。

# 支持说明

应用能力 Android iOS PC 预览效果
小程序 3.12.0 3.12.0 3.12.0
扫码预览
用BoosHi扫码或PC端点击
网页应用 X X X

# 输入

# 输出

# 示例代码

try {
    const updateManager = bz.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      console.log(res.hasUpdate)
    })
    updateManager.onUpdateReady(function () {
      // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
      updateManager.applyUpdate()
    })
    updateManager.onUpdateFailed(function () {
    // 新版本下载失败
      console.log('download error');
    })
    // 每隔一个小时主动检查更新一次
    setInterval(() => updateManager.triggerCheckUpdate(), 60*60*1000)
} catch (error) {
    console.log(`getUpdateManager fail: ${JSON.stringify(error)}`);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
最后更新于 : 6/27/2023, 9:19:51 PM