我做了一些研究,这个问题也出现了,但不是我想要的方式。我正在为客户端构建一个页面,这是一个QR码登陆,这是一个下载应用程序的地方。所以他不需要在页面上打印2个QR码,我想检测当前的操作系统(苹果/安卓/其他[不支持]),并根据该值修改我的元素。

我已经看过脚本“detectmobilebrowsers”,它的目的只是告诉用户是否移动,而我想弄清楚用户正在运行什么操作系统,并建议最好的应用程序版本。

我发现与这个问题类似的其他答案要么过时,要么不可靠(Android平板电脑浏览器没有检测),所以我正在寻找一些新的答案。我怎样才能做到这一点呢?(最好使用jQuery - Javascript - PHP的顺序)。


当前回答

可以使用导航器。获取安装浏览器的操作系统的平台。

function getPlatform() {
   var platform = ["Win32", "Android", "iOS"];

   for (var i = 0; i < platform.length; i++) {

       if (navigator.platform.indexOf(platform[i]) >- 1) {

           return platform[i];
       }
   }
}

getPlatform();

其他回答

您可以测试用户代理字符串:

/**
 * Determine the mobile operating system.
 * This function returns one of 'iOS', 'Android', 'Windows Phone', or 'unknown'.
 *
 * @returns {String}
 */
function getMobileOperatingSystem() {
    var userAgent = navigator.userAgent || navigator.vendor || window.opera;

    // Windows Phone must come first because its UA also contains "Android"
    if (/windows phone/i.test(userAgent)) {
        return "Windows Phone";
    }

    if (/android/i.test(userAgent)) {
        return "Android";
    }

    // iOS detection from: http://stackoverflow.com/a/9039885/177710
    if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
        return "iOS";
    }

    return "unknown";
}
function getOS() {
  var uA = navigator.userAgent || navigator.vendor || window.opera;
  if ((/iPad|iPhone|iPod/.test(uA) && !window.MSStream) || (uA.includes('Mac') && 'ontouchend' in document)) return 'iOS';

  var i, os = ['Windows', 'Android', 'Unix', 'Mac', 'Linux', 'BlackBerry'];
  for (i = 0; i < os.length; i++) if (new RegExp(os[i],'i').test(uA)) return os[i];
}

你现在可以这样使用它:

alert(getOS());

工作代码片段:

函数getOS() { var uA =导航器。userAgent || navigator。window.opera; if (/iPad|iPhone|iPod/.test(uA) && !window.MSStream) || (uA.includes('Mac') && 'ontouchend' in document))返回'iOS'; var, os =[“窗口”,“Android”,“Unix”,“苹果”,“Linux”,“黑莓”); For (i = 0;I < os.length;我+ +)如果(新RegExp (os(我),“我”)。test (uA))返回操作系统(我); } console.log (getOS ());

你也可以在php上使用用户代理来实现这一点:

$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);

if(stripos($userAgent,'android') !== false) { // && stripos($userAgent,'mobile') !== false) {
  header('Location: http://oursite.com/download/yourApp.apk');

exit();

}

如果你的网站使用React Js,请使用https://www.npmjs.com/package/react-device-detect

这是一个简单的js脚本(在html标签),检测设备类型,然后重定向到正确的商店。

<script>
var userAgent = navigator.userAgent || navigator.vendor || window.opera;

    // Huawei Phone must come first because its UA also contains "Android"
    if (userAgent.includes("HUAWEI")) {
       window.location.replace("https://appgallery.huawei.com/app/C101184875");
    } else if (userAgent.includes("Android")) {
        window.location.replace("https://play.google.com/store/apps/details?id=org.telegram.messenger&hl=en&gl=US");
    } else if (userAgent.includes("iPhone")) {
        window.location.replace("https://apps.apple.com/us/app/telegram-messenger/id686449807");
    } else {
        //cant be sure about device, so redirect to home page
        window.location.replace("https://google.ro");
    }
</script>