返回博客

JavaScript 判断是否为 iPad

使用 JavaScript 判断用户设备是否为 iPad 的方法,包括解决 iPad Pro 检测问题。

Mt.r
|

网上找到原因了:StackOverFlow

iPadPro reports navigator.platform the browser as ‘MacIntel’, but that is the same as other platforms.

解决方法

判断是不是多点触控 navigator.maxTouchPoints

function isIOS() {
  if (/iPad|iPhone|iPod/.test(navigator.platform)) {
    return true;
  } else {
    return (
      navigator.maxTouchPoints &&
      navigator.maxTouchPoints > 2 &&
      /MacIntel/.test(navigator.platform)
    );
  }
}

function isIpadOS() {
  return (
    navigator.maxTouchPoints &&
    navigator.maxTouchPoints > 2 &&
    /MacIntel/.test(navigator.platform)
  );
}