# getUpdateManager

获取全局唯一的版本更新管理器,返回 updateManager 对象,用于管理小程序更新。

# 支持说明

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

# 输入

# 输出

返回值:updateManager,该对象的方法列表参见下表:

点击下表中的方法名,查看对应API的支持说明、调用方法

方法 介绍
applyUpdate 强制小程序重启并使用新版本。在小程序新版本下载完成后(即收到 onUpdateReady 回调)调用。
onCheckForUpdate 监听向后台请求检查更新结果事件。客户端在小程序冷启动时自动检查更新,不需由开发者主动触发。线上环境在有更新内容时会触发callback回调
onUpdateFailed 监听小程序更新失败事件。小程序有新版本,客户端主动触发下载(无需开发者触发),下载失败(可能是网络原因等)后回调
onUpdateReady 监听小程序有版本更新事件。客户端主动触发下载(无需开发者触发),下载成功后回调
triggerCheckUpdate 主动触发更新小程序。开发者调用该方法时,首先触发onCheckForUpdate事件;当前运行版本低于线上版本时,会强制更新小程序,若下载成功或本地已存在线上最新包,则会触发onUpdateReady事件,否则触发onUpdateFailed事件。

# 示例代码

try {
    const updateManager = tt.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