js判断移动端APP是否安装

我们在浏览网页的时候,你会看到一个网页下面漂浮着一个提示框“打开APP”或者“下载APP的字样”,如果你的手机已经安装过这个APP,那么网页会提示“打开APP”,如果没有安装,那就会提示“下载APP的字样”  这个从技术角度是如何去实现的呢?下面我给大家分享这块技术,去年公司给国际动漫节做项目的时候,客户就提到这个需求,在点击网页企业的时候 那么直接打开APP(如果已经安装了) 如果没有安装过,直接打开APP页面
下面我把这块的源码分享一下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
if (navigator.userAgent.match(/android/i)) {
      // 通过iframe的方式试图打开APP,如果能正常打开,会直接切换到APP,并自动阻止a标签的默认行为
      // 否则打开a标签的href链接
      var isInstalled;
      //下面是安卓端APP接口调用的地址,自己根据情况去修改
      var ifrSrc =  'cartooncomicsshowtwo://platformapi/startApp? type=0&id=${com.id}&phone_num=${com.phone_num}' ;
      var ifr = document.createElement( 'iframe' );
      ifr.src = ifrSrc;
      ifr.style.display =  'none' ;
      ifr.onload = function() {
      // alert('Is installed.');
      isInstalled =  true ;
      alert(isInstalled);
      document.getElementById( 'openApp0' ).click();};
      ifr.onerror = function() {
          // alert('May be not installed.');
          isInstalled =  false ;
          alert(isInstalled);
      }
      document.body.appendChild(ifr);
      setTimeout(function() {
          document.body.removeChild(ifr);
      }, 1000 );
}
//ios判断
if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i))
      if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i))  {
          //Animation://com.yz.animation
          var isInstalled;
          //var gz = '{"comName":"${com.short_name}","comID":"${com.id}","comPhoneNum":"${com.phone_num}","type":"0"}';
          //var jsongz =JSON.parse(gz);
 
          //下面是IOS调用的地址,自己根据情况去修改
          var ifrSrc =  'Animation://?comName=${com.short_name}&comID=${com.id}&comPhoneNum=${com.phone_num}&type=0' ;var ifr = document.createElement( 'iframe' );
          ifr.src = ifrSrc;
          ifr.style.display =  'none' ;
          ifr.onload = function() {
               // alert('Is installed.');
               isInstalled =  true ;
               alert(isInstalled);
               document.getElementById( 'openApp1' ).click();};
          ifr.onerror = function() {
               // alert('May be not installed.');
               isInstalled =  false ;
               alert(isInstalled);
          }
          document.body.appendChild(ifr);
          setTimeout(function() {
               document.body.removeChild(ifr);
          }, 1000 );
      }
}
大家在做的过程中需要注意两个问题:
1、接口地址一定要写对,大家可以查一下schema协议,通过这个协议调用的
2、在做用安卓的时候  如果用微信扫一扫或者QQ浏览器扫码功能的时候
使用上面的协议会存在问题的就是必须用APK上架到腾讯应用市场上去
源码下载地址:http://download.csdn.net/detail/wangliang198901/8695049

转载请注明:Android开发中文站 » JS检测APP是否安装的情况

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值