uniapp app热更新

1、在app.vue里面获取当前版本号

plus.runtime.getProperty(plus.runtime.appid, wgtinfo => {
                    uni.setStorageSync('version', wgtinfo.version)
                })

2、在首页获取到后端返回的最新版本号做对比(不一定非要是首页,在哪里都行)

getSysVersion() {
                const _this = this;
                //获取缓存中,当前app的版本号
                this.version = uni.getStorageSync('version')
                console.log(this.version);
                this.$u.post('versionUpgrade').then(res => {
                    console.log(this.version);
                    console.log(String(res.data.versionNumber));
                    if (this.version != res.data.versionNumber) {
                        // 强制更新
                        if (res.data.versionDemand == 0) {
                            uni.showModal({
                                title: "发现新版本",
                                content: "确认下载更新",
                                success: (res1) => {
                                    if (res1.confirm == true) {
                                        //当用户确定更新,执行更新,下面方法进行下载app
                                        this.appdownLoad(res.data.versionAddress);
                                    } else {
                                        // 取消后退出应用
                                        if (uni.getSystemInfoSync().platform == 'ios') {
                                            plus.ios.import("UIApplication").sharedApplication()
                                                .performSelector("exit")
                                        } else if (uni.getSystemInfoSync().platform == 'android') {
                                            plus.runtime.quit();
                                        }
                                    }
                                }
                            })
                            // 非强制更新
                        } else if (res.data.versionDemand == 1) {
                            uni.showModal({
                                title: "发现新版本",
                                content: "确认下载更新",
                                success: (res1) => {
                                    if (res1.confirm == true) { //当用户确定更新,执行更新,下面方法进行下载app
                                        this.appdownLoad(res.data.versionAddress);
                                    } else {
                                        return false
                                    }
                                }
                            })
                        }

                    }
                })
            },

3、用户点击确认后开始更新

appdownLoad(url) {
                var that = this;
                uni.showLoading({
                    title: '安装包下载中……',
                    mask: true,
                })
                const downloadTask = uni.downloadFile({
                    url: url, //服务器 app 的下载地址
                    success: (downloadResult) => {
                        uni.hideLoading();
                        console.log(downloadResult)
                        if (downloadResult.statusCode === 200) {
                            uni.showModal({
                                title: '',
                                content: '下载成功,是否允许安装新版本?',
                                confirmText: '确定',
                                success: function(res) {
                                    if (res.confirm == true) {
                                        plus.runtime.install( //安装
                                            downloadResult.tempFilePath, {
                                                force: true
                                            },
                                            function(res) {
                                                utils.showToast('更新成功,重启中');
                                                plus.runtime.restart();
                                            }
                                        );
                                    }
                                }
                            });
                        }
                    }
                });
            },

### UniApp iOS 应用热更新实现方法 #### 版本检测与自动更新逻辑集成 为了实现在iOS平台上对UniApp应用进行热更新,在应用启动文件`app.vue`中需加入版本检测代码[^1]。此部分代码负责检查服务器上的最新版本号以及对应的HBuilderX打包资源链接,一旦发现本地版本低于线上版本,则触发下载并替换现有资源。 ```javascript // app.vue 中的 mounted 钩子函数内执行版本对比操作 mounted() { uni.request({ url: 'https://yourserver.com/api/version', // 获取当前服务端最新的版本信息接口地址 success(res) { const serverVersion = res.data.version; let localVersion = plus.runtime.appid; // 或者其他存储版本号的位置 if (localVersion < serverVersion) { downloadAndReplaceResources(); // 自定义函数用于处理实际的资源拉取及覆盖流程 } }, fail(err){ console.error('Failed to fetch version info:', err); } }); } ``` #### 下载新资源并完成安装过程 当确认存在可更新的内容之后,通过调用`plus.downloader.createDownload()`创建下载任务获取远程zip压缩包形式的新版页面模板和其他静态资产文件,并将其解压至指定目录下以便即时生效: ```javascript function downloadAndReplaceResources(){ var dtask = plus.downloader.createDownload( "http://example.com/latest.zip", {}, function(d, status) { if(status == 200){ unzipFile(d.filename); // 解压已下载好的ZIP档案到合适路径 }else{ alert("Update failed"); } } ); dtask.start(); } function unzipFile(filePath){ plus.io.resolveLocalFileSystemURL('_www/', function(entry){ entry.removeRecursively(function(){ /* 清除旧有数据 */ }, null); plus.zip.uncompress( filePath, '_www/', function(e){ reloadApplication(); }, // 更新完毕重启程序使更改立即可见 function(error){ console.log(JSON.stringify(error)); } ); }); } function reloadApplication(){ location.reload(true); // 强制刷新整个网页以加载新的HTML/CSS/JS等前端组件 } ``` 上述脚本展示了如何利用Plus API来管理从网络接收的数据流直至最终部署于客户端设备上的一系列动作。值得注意的是,对于苹果商店分发的应用而言,任何涉及修改应用程序内部结构的行为都可能违反其审核指南,因此建议仅针对企业级内部发行或是测试环境采用此类方案。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值