You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

优化设备识别代码,实现iPad跳转App Store及应用唤醒功能

解决方案代码
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>跳转中...</title>
    <script>
function getMobileOperatingSystem() {
  const userAgent = navigator.userAgent || navigator.vendor || window.opera;

  // Windows Phone 优先检测(其UA包含Android字段)
  if (/windows phone/i.test(userAgent)) {
    return "Windows Phone";
  }

  // Android 设备检测
  if (/android/i.test(userAgent)) {
    return "Android";
  }

  // iOS/iPadOS 兼容检测:覆盖旧版iOS UA和新版iPadOS伪装Mac的情况
  const isLegacyIOS = /iPad|iPhone|iPod/.test(userAgent) && !window.MSStream;
  const isiPadOS = navigator.platform === 'MacIntel' && window.navigator.maxTouchPoints > 1 && !/Macintosh/.test(userAgent);
  if (isLegacyIOS || isiPadOS) {
    return "iOS";
  }

  // 桌面设备分支
  return "Desktop";
}
    </script>
    <script>
function DetectAndServe() {
  // 可根据实际应用修改配置
  const appConfig = {
    ios: {
      deepLink: "googledocs://", // iOS应用深度链接协议
      storeLink: "https://apps.apple.com/us/app/google-docs-sync-edit-share/id842842640"
    },
    android: {
      deepLink: "intent://docs.google.com/#Intent;scheme=googledocs;package=com.google.android.apps.docs.editors.docs;end", // Android Intent唤起链接
      storeLink: "https://play.google.com/store/apps/details?id=com.google.android.apps.docs.editors.docs"
    },
    desktop: "https://docs.google.com/",
    windowsPhone: "https://docs.google.com/"
  };

  const os = getMobileOperatingSystem();
  let targetUrl = appConfig.desktop;
  let attemptOpenApp = false;

  switch(os) {
    case "iOS":
      targetUrl = appConfig.ios.storeLink;
      attemptOpenApp = true;
      break;
    case "Android":
      targetUrl = appConfig.android.storeLink;
      attemptOpenApp = true;
      break;
    case "Windows Phone":
      targetUrl = appConfig.windowsPhone;
      break;
    case "Desktop":
      targetUrl = appConfig.desktop;
      break;
  }

  if (attemptOpenApp) {
    const startTime = Date.now();
    // iOS用iframe唤起应用,避免直接跳转导致的页面异常
    if (os === "iOS") {
      const iframe = document.createElement("iframe");
      iframe.style.display = "none";
      iframe.src = appConfig.ios.deepLink;
      document.body.appendChild(iframe);
      setTimeout(() => document.body.removeChild(iframe), 1000);
    } else {
      // Android直接跳转Intent链接
      window.location.href = appConfig.android.deepLink;
    }

    // 超时检测:若1.5秒内页面未隐藏,说明唤起失败,跳转应用商店
    setTimeout(() => {
      if (Date.now() - startTime < 1800 && document.visibilityState !== "hidden") {
        window.location.href = targetUrl;
      }
    }, 1500);
  } else {
    // 非移动设备直接跳转预设链接
    window.location.href = targetUrl;
  }
}
    </script>
</head>
<body onload="DetectAndServe()">
    <div style="text-align: center; margin-top: 50px;">正在跳转,请稍候...</div>
</body>
</html>

核心问题修复说明

  • iPadOS识别修复:新增针对新版iPadOS的检测逻辑,通过navigator.platform结合触控点数量判断,解决iPad Safari被识别为PC的问题。
  • 应用唤起+失败 fallback:
    • iOS使用应用深度链接通过iframe唤起,避免直接跳转导致的页面刷新;
    • Android使用Intent链接直接唤起应用;
    • 加入超时检测,若唤起失败则自动跳转至对应应用商店。
  • 保留PC分支:明确区分桌面设备,跳转预设PC端链接,方便后续项目复用。

内容的提问来源于stack exchange,提问作者John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 21:43:17