h5端实现判断是否安装app和唤起app

需求

判断是否有安装app, 安装了则跳转应用, 没有安装则跳转下载页

参考文章

通过浏览器判断是否安装APP
H5判断是否安装某款app,打开app或跳转到app下载页面

完整代码

<template>
  <iframe
    :value="iframeSrc"
    ref="iframe"
    style="display: none; width: 0; height: 0"
  ></iframe>
  <div class="button" @click="openApp">
    <span>test</span>
  </div>
</template>
<script setup lang="ts">
import { ref } from "vue";
const iframeSrc = ref("");
const iframe = ref(null);
const iframeConfig = {
  iosScheme: "com.example.app://",
  iosStoreLink: "https://apps.apple.com/app/",
  androidScheme: "com.example.app://",
  androidStoreLink: "https://play.google.com/store/apps/details?",
  fallbackUrl: "https:/www.miti.chat",
};
function openApp() {
  const userAgent = navigator.userAgent;
  const startTime = new Date().getTime();
  console.log(startTime);
  console.log(userAgent);

  if (/iPhone|iPod|iPad/i.test(userAgent)) {
    iframeSrc.value = iframeConfig.iosScheme;

    setTimeout(() => {
      const endTime = new Date().getTime();
      if (endTime - startTime < 2000) {
        window.location.href = iframeConfig.iosStoreLink;
      }
    }, 1000);
  } else if (/android/i.test(userAgent)) {
    iframeSrc.value = iframeConfig.androidScheme;

    setTimeout(() => {
      const endTime = new Date().getTime();
      if (endTime - startTime < 2000) {
        window.location.href = iframeConfig.androidStoreLink;
      }
    }, 1000);

    iframeSrc.value = "";
  } else {
    window.location.href = iframeConfig.fallbackUrl;
  }
  console.log(iframeSrc.value);
}
</script>
const CONFIG = {
  iosScheme: "com.example.app://", // 替换成你的 iOS 应用 URL
  iosDownload: "https://apps.apple.com/app/example-app-id", // 替换成你的 iOS 下载页面 URL
  androidScheme: "com.example.app://", // 替换成你的 Android 应用 URL
  androidDownload:	
    "https://play.google.com/store/apps/details?id=com.example.app", // 替换成你的 Android 下载页面 URL
};
function openApp() {
  const userAgent = navigator.userAgent;
  const iframe: HTMLIFrameElement = document.createElement("iframe");
  iframe.style.display = "none"; // 确保 iframe 不会显示在页面上
  const startTime = new Date().getTime();
  let hasApp = true; // 假设设备上已安装应用

  if (/iPhone|iPod|iPad/i.test(userAgent)) {
    iframe.src = CONFIG.iosScheme; // 尝试打开 iOS 应用的 URL

    setTimeout(() => {
      const endTime = new Date().getTime();
      if (endTime - startTime < 2000) {
        hasApp = false;
        window.location.href = CONFIG.iosDownload; // 跳转到 iOS 下载页面的 URL
      }
      document.body.removeChild(iframe);
    }, 1000);
  } else if (/android/i.test(userAgent)) {
    iframe.src = CONFIG.androidScheme; // 尝试打开 Android 应用的 URL

    setTimeout(() => {
      const endTime = new Date().getTime();
      if (endTime - startTime < 2000) {
        hasApp = false;
        window.location.href = CONFIG.androidDownload; // 跳转到 Android 下载页面的 URL
      }
      document.body.removeChild(iframe);
    }, 1000);
  }
}
export default openApp;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值