js h5页面调起App(IOS 和android)的实现方法

js h5页面调起App(IOS 和android)的实现方法

第一次写这个:我们的app是由原声和h5组成的,当用户使用浏览器打开我们的页面时我们会引导用户使:打开App 下载App
刚开始我是使用定时器来实现的,当用户点击了打开App经过一定时间之后没有反应,则我们认为她调起App失败,跳转到下载页面,但是这个存在一个问题,起始已经调起App了但是用户没有允许打开App过了一定时间之后下载的页面还是会弹出来,这样就不合理了,已经有App了还提示要下载。同时还有个问题我的手机是r11 7.1.1系统自带浏览器一直调不起App(上面实现的方法是当浏览器加载h5页面时就会执行),因此我把调起App和下载的地址分开了,测试了一些手机兼容性还不错,都可以调起和实现下载。用按钮来实现。
具体代码如下:
//下面为用户扫描二维码时进行的跳转

 var userAgent = window.navigator.userAgent;
        if (userAgent.match(这里用来区别用户是在浏览器打开还是App内打开) == null) {//当用户在浏览器中打开时
            document.getElementById("app_button").style.display = "block";//下载打开按钮显示出来
            if (userAgent.match(/MicroMessenger/) != null) {//用微信扫描时
                document.getElementById("weixin").style.display = "block";
                //下面根据不同的手机显示不同的提示语
                if (mui.os.ios) {
                    document.getElementById("addroid_weixin").style.display = "none";
                    document.getElementById("ios_weixin").style.display = "inline";
                } else if (mui.os.android) {
                    document.getElementById("ios_weixin").style.display = "none";
                    document.getElementById("addroid_weixin").style.display = "inline";
                }
                document.getElementById("app_button").style.display = "none";
            }
        }
        var parameter = window.location.href;
        var ios_parmeter = parameter.substring(7, parameter.length);
        document.getElementById("open_app").addEventListener('tap', function () {
            if (mui.os.ios) {
                window.location.href = "打开IOS的名字://" + ios_parmeter;
            } else if (mui.os.android) {
                window.location.href = "打开App的名字://hrst.com/android?data=" + parameter;
            }
        });
        document.getElementById("download_upgrade").addEventListener('tap', function () {
            if (mui.os.ios) {
            //跳转IOS下载商城地址
                window.location.href = "https://itunes.apple.com/cn/app/";
            } else if (mui.os.android) {
                var downLoad = '@ViewBag.downLoad';
                window.location.href = "android的下载地址";
            }
        });

猜你喜欢

转载自blog.csdn.net/qq_35500233/article/details/84581194