H5如何唤醒APP

499 阅读1分钟

H5如何唤醒APP

  1. 先判断环境

    1. var is_android =agent.indexOf("Android") > -1 || agent.indexOf("Linux") > -1; //g
       var is_ios = !!agent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端F
      
  2. 准备好APP的schema和下载地址

  3. 先跳转schema【无法判断是否打开】

  4. 常用方式:

    1. 跳转schema不管有没有打开就弹窗提示用户下载
  5. var isWeixin = !!/MicroMessenger/i.test(agent);
        var andDownload = "XXX";
        var iosDownload ="XXX";
        var is_android =agent.indexOf("Android") > -1 || agent.indexOf("Linux") > -1; //g
        var is_ios = !!agent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端F
        var scheme = getSchemaUrl();
        if (pageContent) {
          pageContent.style.paddingTop = 1.3 + "rem";
        }
        // 拼接scheme
        function getSchemaUrl() {
          var str = "";
          var url = window.location.href;
          var index = url.lastIndexOf("/");
          str = url.substring(index + 1, url.length - 5);
          console.log("str", str);
          return "XXX" + str;
        }
        function gotoDonload(){
          if (is_android) {
            window.location.href = andDownload;
          } else if (is_ios) {
            window.location.href = iosDownload;
          }
        }
        // 绑定事件
        function bindEvent() {
          wrapper.addEventListener("click", function () {
            if (isWeixin) {
              wrapper.style.display = "none";
              openWechat.style.display = "none";
            }
          });
          // 在APP中打开
          openBtn.addEventListener("click", function () {
            if (isWeixin) {
              wrapper.style.display = "block";
              openWechat.style.display = "block";
            } else {
              // 打开蒙层
              wrapper.style.display = "block";
              wrapperFloat.style.display = "block";
              floatOpen.style.display = "block";
              // 尝试打开scheme
              window.location.href = scheme;
              setTimeout(() => {
                floatOpen.style.display = "none";
                floatOption.style.display = "block";
              }, 2000);
            }
          });
          // 继续阅读
          continueBtn.addEventListener("click", function () {
            wrapper.style.display = "none";
            wrapperFloat.style.display = "none";
            floatOpen.style.display = "none";
            floatOption.style.display = "none";
          });
          // 下载APP
          downBtn.addEventListener("click", function () {
            gotoDonload();
          });
          downWechat.addEventListener("click", function (e) {
            e.stopPropagation()
            gotoDonload();
          });