title: UpdateManager header: develop nav: api
sidebar: UpdateManager
解释:管理更新,swan.getUpdateManager返回值。
方法参数
无
示例
扫码体验
请使用百度APP扫码
代码示例
- 在 swan 文件中
<view class="container"><view class="card-area"><view class="top-description border-bottom">是否下载最新版本</view><button type="primary" bindtap="applyUpdate">button</button></view></view>
Page({onLoad(){const updateManager = swan.getUpdateManager();this.updateManager = updateManager;},applyUpdate() {this.updateManager.onCheckForUpdate(function (res) {// 请求完新版本信息的回调console.log("res", res.hasUpdate);if(!res.hasUpdate){swan.showModal({title: '更新提示',content: '无可用更新版本',});}else {this.updateManager.onUpdateReady(function (res) {swan.showModal({title: '更新提示',content: '新版本已经准备好,是否重启应用?',success(res) {if (res.confirm) {// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启updateManager.applyUpdate();}}});});this.updateManager.onUpdateFailed(function (err) {// 新的版本下载失败swan.showModal({title: '更新提示',content: '新版本下载失败,请稍后再试'});});}});}});
