H5唤醒APP

Android使用iframe唤起,ios采用window.location.href唤起更合适一点。

 

1.ios系统:

 在wap中唤起app其实应用最最广泛的并不是Universal Link,而是直接Schema跳转

 window.location.href = "taobao://fulushuka.tmall.com/shop/view_shop.htm?spm=a230r.7195193.1997079397.2.5yhUYD";

 Schema无法判断是否安装App,如果用户手机没有安装要唤醒的App,需要提示用户去下载。

 目前没有好的办法去判断ios是否唤醒成功,如果成功就跳转页面,不执行后面setTimeout()方法;

 如果没有跳转成功,会执行setTimeout(),在这里提示用户去下载想唤醒的App。这里有点投机取巧,并不是很完美。

 

2.安卓系统:

  需要在Dom中添加iframe节点,唤醒App后,删除此节点。

// 给原生传递数据
function loadURL(url) {
  var iFrame;
  var u = navigator.userAgent;
  var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
  var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  if (isAndroid) {
    //安卓终端使用iframe
    iFrame = document.createElement("iframe");
    iFrame.setAttribute("src", url);
    iFrame.setAttribute("style", "display:none;");
    iFrame.setAttribute("height", "0px");
    iFrame.setAttribute("width", "0px");
    iFrame.setAttribute("frameborder", "0");
    document.body.appendChild(iFrame);
    // 发起请求后这个 iFrame 就没用了,所以把它从 dom 上移除掉
    iFrame.parentNode.removeChild(iFrame);
    iFrame = null;
  } else if (isiOS) {
    //iOS终端直接页面跳转
    window.location.href = url;
    // 如果用户没有安装淘宝APP,则提示用户去安装淘宝
    setTimeout(() => {
        alert('请去应用市场下载App'); // 这里可以自行写一个延时关闭的弹窗,也可以跳转至app下载地址,我这里就偷懒了
    }, 2000);
  } else {
    window.location.href = url;
  }
}

 

3.微信端是不能唤醒的:

  需要加一个蒙层,引导用户去浏览器中打开。判断微信端方法:

function isInWx(){
    var agent = window.navigator.userAgent.toLowerCase();
    return agent.match(/MicroMessenger/i) == 'micromessenger';
}

 

 

 

参考链接:https://www.jb51.net/article/117959.htm

posted @ 2018-06-27 18:12  清风晰心  阅读(5250)  评论(2编辑  收藏  举报