Home  >  Article  >  Web Front-end  >  How to upgrade an H5 hybrid developed app

How to upgrade an H5 hybrid developed app

不言
不言Original
2018-06-12 17:18:431553browse

This article mainly introduces how to upgrade the H5 hybrid development app. The content is quite good. Now I will share it with you and give it as a reference.

After the development of our app is completed, it is inevitable that the product will be upgraded in the future, so we hope that the app will be automatically upgraded on the customer's mobile phone, which can be divided into automatic upgrade and manual upgrade.

Automatic upgrade: usually when the customer app opens the homepage for the first time.

Manual upgrade: Provide an upgrade entrance in the app interface.

The interface effect is demonstrated as follows:

The code is actually very simple, but it needs to be processed separately for ios and android. The basic idea is to obtain the app version number of the local machine, and then compare it with the app version number on the server. If it is less than the app version number on the server, then perform an update operation.

var btn = ["确定升级", "取消"];
//获取app系统更新[是否手动点击获取更新]
function appUpdate(ismanual) {
    console.log('appUpdate');
    mui.plusReady(function () {
        plus.runtime.getProperty(plus.runtime.appid, function (inf) {
            ver = inf.version;
            console.log('ver:' + ver);
            var url = config.GetAppVersion;
            var client;
            var ua = navigator.userAgent.toLowerCase();
            if (/iphone|ipad|ipod/.test(ua)) {    //苹果手机            
                mui.ajax({
                    type: "get",
                    dataType: 'json',
                    url: "https://itunes.apple.com/lookup?id=1318127518",//获取当前上架APPStore版本信息
                    data: {
                        id: 131812xxxx //APP唯一标识ID
                    },
                    contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
                    success: function (data) {
                        console.log('data:' + JSON.stringify(data));
                        var resultCount = data.resultCount;
                        for (var i = 0; i < resultCount; i++) {
                            var normItem = data.results[i].version;
                            console.log(&#39;normItem:&#39; + normItem)
                            if (normItem > ver) {
                                var _msg = "发现新版本:V" + normItem;
                                //plus.nativeUI.alert("发现新版本:V" + normItem);
                                mui.confirm(_msg, &#39;升级确认&#39;, btn, function (e) {
                                    if (e.index == 0) { //执行升级操作
                                        document.location.href = &#39;https://itunes.apple.com/cn/app/san-gu-hui/id131812xxxx?mt=8&#39;; //上新APPStore下载地址
                                    }
                                });
                                return;
                            } 
                        }
                        if (ismanual) {
                            mui.toast(&#39;当前版本号已是最新&#39;);
                        }
                        return;
                    }
                });
            } else if (/android/.test(ua)) {
                mui.ajax(url, {
                    data: {
                        apkVersion: ver,
                    },
                    dataType: &#39;json&#39;,
                    type: &#39;get&#39;,
                    timeout: 10000,
                    success: function (data) {
                        //console.log(&#39;data:&#39;+JSON.stringify(data))
                        if (data.StatusCode = 200 && data.Data > ver) {
                            //mui.toast("发现新版本:V" + data.Data);//获取远程数据库中上新andriod版本号 
                            var _msg="发现新版本:V" + data.Data;
                            mui.confirm(_msg, &#39;升级确认&#39;, btn, function (e) {
                                if (e.index == 0) { //执行升级操作
                                    plus.nativeUI.toast("正在准备环境,请稍后!");
                                    var dtask = plus.downloader.createDownload(config.apkUrl, {}, function (d, status) {
                                        if (status == 200) {
                                            var path = d.filename;//下载apk
                                            plus.runtime.install(path); // 自动安装apk文件
                                        } else {
                                            plus.nativeUI.alert(&#39;版本更新失败:&#39; + status);
                                        }
                                    });
                                    dtask.start();
                                }
                            });
                        } else {
                            console.log(&#39;当前版本号已是最新&#39;);
                            if (ismanual) {
                                mui.toast(&#39;当前版本号已是最新&#39;);
                            }
                            return;
                        }
                    },
                    error: function (xhr, type, errerThrown) {
                        if (ismanual) {
                            mui.toast(&#39;网络异常,请稍候再试&#39;);
                        }
                    }
                });
            }
        });
    });
}

Our ios applications are published in the Apple App Store, while android applications are deployed directly on our own servers (such as IIS servers), because there are too many android application markets. In that case Upgrading the version every time is a very troublesome thing. Every time you release a version, you have to go to all Android application markets to submit updates.

It should be noted that when calling this method using manual update and automatic update, different parameters must be passed in, because in automatic update, if the system detects that the current version is already the latest version, it will not be displayed on the client. Display, and if it is updated manually, if it is already the latest version, the customer needs to be prompted.

Automatic update call: appUpdate();//Detect app update

Manual update call: appUpdate(true);//Detect app update

That’s all for this article Content, I hope it will be helpful to everyone’s learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to solve the problem of WeChat directly opening the local app through the H5 page

##

The above is the detailed content of How to upgrade an H5 hybrid developed app. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn