h5 唤起 app(ios,and),没下载则跳转下载链接

8 篇文章 0 订阅
3 篇文章 0 订阅
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
    <meta charset="utf-8">
    <title>唤醒app</title>
    <div id="btn"
        style="font-size: 2.2rem;margin: 300px auto;display: block;color: #ffffff;width: 300px;height: 100px;line-height: 100px;background: rgb(213, 0, 9);text-align: center;border-radius: 15px;">
        点击打开</div>
</head>

<body>

    <script>
    	
 function detectVersion() {
          let isAndroid,isIOS,isIOS9,version,
              u = navigator.userAgent,
              ua = u.toLowerCase();

          if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {   //android终端或者uc浏览器
              //Android系统
              isAndroid = true
          }

          if(ua.indexOf("like mac os x") > 0){
              //ios
              var regStr_saf = /os [\d._]*/gi ;
              var verinfo = ua.match(regStr_saf) ;
              version = (verinfo+"").replace(/[^0-9|_.]/ig,"").replace(/_/ig,".");
          }
          var version_str = version+"";
          if(version_str != "undefined" && version_str.length >0){
              version = parseInt(version)
              if(version>=8){
                  // ios9以上
                  isIOS9 = true
              }
              else{
                  isIOS = true
              }
          }
          return {isAndroid,isIOS,isIOS9}
        }

        // 判断手机上是否安装了app,如果安装直接打开url,如果没安装,执行callback
        function openApp(url,callback) {
            let {isAndroid,isIOS,isIOS9} = detectVersion()
            if(isAndroid || isIOS){
                var timeout, t = 4000, hasApp = true;  
                var openScript = setTimeout(function () {  
                    if (!hasApp) {
                        callback && callback()
                    }
                    document.body.removeChild(ifr);  
                }, 5000)  
                
                var t1 = Date.now();  
                var ifr = document.createElement("iframe");  
                ifr.setAttribute('src', url);  
                ifr.setAttribute('style', 'display:none');  
                document.body.appendChild(ifr);  

                timeout = setTimeout(function () {  
                    var t2 = Date.now();  
                    if (t2 - t1 < t + 100) {  
                        hasApp = false;
                    }
                }, t);
            }

            if(isIOS9){
                location.href = url;
                setTimeout(function() {   
                    callback && callback()
                }, 250);
                setTimeout(function() {
                    location.reload();
                }, 1000);
            }  
        }
        
        const btnNode = document.getElementById('btn')
        btnNode.addEventListener('click', () => {
        // 跳转 iosApp
        		openApp("bangbianli://www.zksr.cn?username=IM/czpDurUmGkRUIqvEh/g==",goConfirmAddr)
        })
        // 跳转下载
        function goConfirmAddr(){
            window.location.href = 'https://apps.apple.com/cn/app/%E5%B8%AE%E4%BE%BF%E5%88%A9/id1276118453'
        }

    </script>
</body>

</html>


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值