多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# UpdateManager ### UpdateManager UpdateManager 对象,用来管理更新,可通过 [wx.getUpdateManager](/read/wechat-game-api/327.md) 接口获取实例。 #### 方法 ##### UpdateManager.applyUpdate() 当小程序新版本下载完成后(即收到 `onUpdateReady` 回调),强制小程序重启并使用新版本 ##### UpdateManager.onCheckForUpdate(function callback) 监听向微信后台请求检查更新结果事件。微信在小程序冷启动时自动检查更新,不需由开发者主动触发。 ##### UpdateManager.onUpdateReady(function callback) 监听小程序有版本更新事件。客户端主动触发下载(无需开发者触发),下载成功后回调 ##### UpdateManager.onUpdateFailed(function callback) 监听小程序更新失败事件。小程序有新版本,客户端主动触发下载(无需开发者触发),下载失败(可能是网络原因等)后回调 #### 示例代码 ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="kwd">const</span><span class="pln"> updateManager </span><span class="pun">=</span><span class="pln"> wx</span><span class="pun">.</span><span class="pln">getUpdateManager</span><span class="pun">()</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">updateManager</span><span class="pun">.</span><span class="pln">onCheckForUpdate</span><span class="pun">(</span><span class="hljs-params"><span class="kwd">function</span></span><span class="pln"> </span><span class="pun">(</span><span class="hljs-params"><span class="pln">res</span></span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="com">// 请求完新版本信息的回调</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="pln">res</span><span class="pun">.</span><span class="pln">hasUpdate</span><span class="pun">)</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">updateManager</span><span class="pun">.</span><span class="pln">onUpdateReady</span><span class="pun">(</span><span class="hljs-params"><span class="kwd">function</span></span><span class="pln"> </span><span class="pun">(</span><span class="hljs-params"></span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> wx</span><span class="pun">.</span><span class="pln">showModal</span><span class="pun">({</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> title</span><span class="pun">:</span><span class="pln"> </span><span class="str">'更新提示'</span><span class="pun">,</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> content</span><span class="pun">:</span><span class="pln"> </span><span class="str">'新版本已经准备好,是否重启应用?'</span><span class="pun">,</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> success</span><span class="pun">:</span><span class="pln"> </span><span class="hljs-params"><span class="kwd">function</span></span><span class="pln"> </span><span class="pun">(</span><span class="hljs-params"><span class="pln">res</span></span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="pln">res</span><span class="pun">.</span><span class="pln">confirm</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="com">// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> updateManager</span><span class="pun">.</span><span class="pln">applyUpdate</span><span class="pun">()</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">}</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">}</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="pun">})</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">updateManager</span><span class="pun">.</span><span class="pln">onUpdateFailed</span><span class="pun">(</span><span class="hljs-params"><span class="kwd">function</span></span><span class="pln"> </span><span class="pun">(</span><span class="hljs-params"></span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="com">// 新版本下载失败</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> </ol> ``` #### Tips - 微信开发者工具上可以通过「编译模式」下的「下次编译模拟更新」开关来调试 - 小程序开发版/体验版没有「版本」概念,所以无法在开发版/体验版上测试更版本更新情况 > 原文: <https://developers.weixin.qq.com/minigame/dev/api/update/UpdateManager.html>