QQ登录

只需要一步,快速开始

APP扫码登录

只需要一步,快速开始

手机号码,快捷登录

查看: 2057|回复: 0

[HTML/CSS/JS] h5页面唤起app,如果没安装就跳转下载(iOS和Android)

[复制链接]

等级头衔

积分成就    金币 : 2810
   泡泡 : 1516
   精华 : 6
   在线时间 : 1245 小时
   最后登录 : 2024-5-17

丰功伟绩

优秀达人突出贡献荣誉管理论坛元老

联系方式
发表于 2022-10-19 09:49:24 | 显示全部楼层 |阅读模式
       浏览器和app没有通信协议,所以h5不知道用户的手机释放安装了app。因此只能是h5去尝试唤起app,若不能唤起,引导用户去下载我们的app。微信里屏蔽了 schema 协议,如果在微信中打开h5,则会提示用户在浏览器中打开。# p" {# e* [: l
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <title></title>
  5.     <meta charset="utf-8" />
  6.     <meta
  7.       content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no;"
  8.       name="viewport"
  9.     />
  10.     <script>
  11.       // 检查是否安装app function openApp(e){
  12.       var u = window.navigator.userAgent;
  13.       var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器 var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
  14.       if(isAndroid){ android();
  15.       }
  16.       if(isiOS){ ios();
  17.       }
  18.       }
  19.       function android(){
  20.       var _clickTime = new Date().getTime();
  21.       window.location.href = '??????????????????'; /***打开app的协议,有安卓同事提供***/
  22.       //启动间隔20ms运行的定时器,并检测累计消耗时间是否超过3000ms,超过则结束 var _count = 0, intHandle;
  23.       intHandle = setInterval(function(){
  24.       _count++;
  25.       var elsTime = new Date().getTime() - _clickTime; if (_count>=100 || elsTime > 3000 ) { console.log(_count)
  26.       console.log(elsTime) clearInterval(intHandle);
  27.       //检查app是否打开
  28.       if ( document.hidden || document.webkitHidden) {
  29.       // 打开了
  30.       window.close();
  31.       } else {
  32.       // 没打开
  33.       alert('没打开')
  34.       // window.location.href = "??????????????";//下载链接
  35.       }
  36.       }
  37.       }, 20);
  38.       }
  39.       function ios(){
  40.       var _clickTime = +(new Date());
  41.       var ifr = document.createElement("iframe");
  42.       ifr.src = "??????????????"; /***打开app的协议,有ios同事提供***/ ifr.style.display = "none";
  43.       document.body.appendChild(ifr);
  44.       //启动间隔20ms运行的定时器,并检测累计消耗时间是否超过3000ms,超过则结束 var _count = 0, intHandle;
  45.       intHandle = setInterval(function(){
  46.       _count++;
  47.       var elsTime = +(new Date()) - _clickTime; console.log(_count,elsTime,+(new Date()),_clickTime) if (_count>=100 || elsTime > 3000 ) {
  48.       clearInterval(intHandle); document.body.removeChild(ifr);
  49.       //检查app是否打开
  50.       if ( document.hidden || document.webkitHidden) {
  51.       // 打开了
  52.       window.close();
  53.       } else {
  54.       // 没打开
  55.       alert('没打开')
  56.       // window.location.href = "???????????????";//下载链接
  57.       }
  58.       }
  59.       },20);
  60.       }
  61.       var objbtn=document.getElementById('btn'); objbtn.onclick=function(){
  62.       openApp()
  63.       }
  64.     </script>
  65.   </head>
  66.   <body style="background-color: #fff">
  67.     <!--测试app调起-->
  68.     <div
  69.       id="btn"
  70.       style="
  71.         font-size: 40px;
  72.         background: blue;
  73.         color: #fff;
  74.         height: 56px;
  75.         text-align: center;
  76.       "
  77.     >
  78.       btn
  79.     </div>
  80.     <!--测试app调起over-->
  81.   </body>
  82. </html>
      重要的是安装了app打开app,但是不能让用户在点击确定的时候,跳转到下载页。没安装app的用户,不能等待太久,还不到app下载页。 若通过url scheme 打开app成功,那么当前h5会进入后台,通过计时器会有明显延迟。利用时间来判断。
+ d# d; P; F. U( N( }& K       由于安卓手机,页面进入后台,定时器setTimeout仍会不断运行,所以这里使用setInterval,较小间隔时间重复多次。来根据累计时间判断。document.hidden对大于4.4的webview支持很好,为页面可见性api。( {5 k4 Y. A+ n
       window.location.href='rtjr://app.rongtuojinrong.com?flag=23&pid={$pid}';js无法调起这种协议的url。) S& }# C) d- ?
       <a href="rtjr://app.rongtuojinrong.com?flag=23&pid={$pid}" ></a>只能用 a标签,或者iframe。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|paopaomj.COM ( 渝ICP备18007172号 )

GMT+8, 2024-5-18 14:58

Powered by paopaomj X3.4 © 2016-2024 sitemap

快速回复 返回顶部 返回列表