【问题标题】:Detecting iOS / Android Operating system检测 iOS / Android 操作系统
【发布时间】:2014-03-11 14:23:17
【问题描述】:

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

我查看了脚本“detectmobilebrowsers”,该脚本旨在判断用户是否是移动设备,而我想弄清楚用户正在运行什么操作系统并建议最佳应用程序版本.

我发现与此问题类似的其他答案似乎已过时或不可靠(未检测到 Android 平板电脑浏览器),因此我正在寻找新的东西。我怎样才能做到这一点? (最好按顺序使用 jQuery - Javascript - PHP)。

【问题讨论】:

  • 用户代理没有告诉你你需要知道什么?
  • 这个问题已经在这里解决了:*.com/questions/3514784/…
  • @gretro,描述如果用户是移动的,而不是他们正在运行的操作系统。 Babak,像 navigator.platform 这样的解决方案吗?我不熟悉用户代理。无论版本如何,我如何确保它适用于所有 android 设备?
  • 没有保证可以检测到它的方法,因为用户代理你唯一可以继续的事情。在这里查看更多信息...whatsmyos.com
  • @Alexander Lozada :在接受的答案中,他们基本上测试它是否是 iPhone、iPod、Android 设备或任何返回 true 的设备。只需保留您想要的,例如 if( /Android/i.test(navigator.userAgent) ) { // some code.. } 将仅对 Android 用户代理返回 true。

标签: javascript jquery browser-detection


【解决方案1】:

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

/**
 * 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://*.com/a/9039885/177710
    if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
        return "iOS";
    }

    return "unknown";
}

【讨论】:

  • 太棒了。使用 Chrome 的移动模式,您可以在桌面浏览器上进行测试。
  • @feeela 有时该功能类似于能够安装 apk,这是无法检测到的。
  • 我已经添加了else if (userAgent.match(/Windows Phone/i)) { return 'WindowsPhone'; } 之前 Android else if 来检测 Windows Phone。到目前为止似乎工作正常。
  • 来自*.com/a/9039885/177710:在检查iOS 时,我们还需要验证!window.MSStream 以避免IE11 被视为iOS ;-)
  • 无法正常工作的 iPad Pro:返回:Safari: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15
【解决方案2】:

此问题已在此处解决:What is the best way to detect a mobile device in jQuery?

在接受的答案中,他们基本上测试它是 iPhone、iPod、Android 设备还是返回 true 的任何设备。只需保留您想要的,例如 if( /Android/i.test(navigator.userAgent) ) { // some code.. } 将仅对 Android 用户代理返回 true。

但是,用户代理并不是真正可靠的,因为它们可以更改。最好的事情仍然是为所有移动平台开发通用的东西。

【讨论】:

  • 如果它在 jQuery 中,那么它与 vanilla JS 不同。它也可能在 C 中。
【解决方案3】:

可以使用 navigator.platform 来获取安装浏览器的操作系统。

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();

【讨论】:

【解决方案4】:

解决方案 1:用户代理嗅探

对于 Android 和 iPhone:

if( /Android|webOS|iPhone|iPad|iPod|Opera Mini/i.test(navigator.userAgent) ) {
 // run your code here
}

如果您想检测包括黑莓和 Windows phone 在内的所有移动设备,那么您可以使用这个综合版本:

var deviceIsMobile = false; //At the beginning we set this flag as false. If we can detect the device is a mobile device in the next line, then we set it as true.


if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|ipad|iris|kindle|Android|Silk|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(navigator.userAgent) 
    || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(navigator.userAgent.substr(0,4))) {
   deviceIsMobile = true;
}

if(deviceIsMobile){
    // run your code here
}

缺点:用户代理字符串随着新手机和品牌的出现而不断变化和更新。因此,如果您想支持所有移动设备,则需要更新此列表。

方案二:移动端检测JS库

您可以使用mobile detect JS library 来执行此操作。

缺点:这些基于 JavaScript 的设备检测功能可能仅适用于最新一代的智能手机,例如 iPhone、Android 和 Palm WebOS 设备。这些设备检测功能可能不适用于对 JavaScript 支持较差的旧智能手机,包括旧的 BlackBerry、PalmOS 和 Windows Mobile 设备。

【讨论】:

    【解决方案5】:

    使用cordova-device-plugin,你可以检测到

    device.platform
    

    对于 android 将是“Android”,对于 windows 将是“windows”。适用于设备,以及在浏览器上模拟时。这是一个将显示设备值的 toast:

        window.plugins.toast.showLongTop(
        'Cordova:     ' + device.cordova + '\n' +
        'Model:       ' + device.model + '\n' +
        'Platform:    ' + device.platform + '\n' +
        'UUID:        ' + '\n' +
                          device.uuid + '\n' +
        'Version:     ' + device.version + '\n' +
        'Manufacturer ' + device.manufacturer + '\n' +
        'isVirtual    ' + device.isVirtual + '\n' +
        'Serial       ' + device.serial);
    

    【讨论】:

      【解决方案6】:

      您还可以创建Firbase Dynamic links,它将根据您的要求工作。它支持多个平台。该链接可以手动创建,也可以通过编程创建。然后,您可以将此链接嵌入到二维码中。

      如果安装了目标应用程序,该链接会将用户重定向到应用程序。如果未安装,它将重定向到 Play 商店/应用商店/任何其他配置的网站。

      【讨论】:

        【解决方案7】:

        您也可以通过 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();
        

        }

        【讨论】:

          【解决方案8】:

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

          【讨论】:

          • 请注意,对于仅检查 iOS 与 Android 而言,这有点重导入 (~23kb)。
          • @jessepinho:是的。事后看来,我不会这样做以保持我的构建轻。但它确实有助于让事情顺利进行。
          【解决方案9】:

          对于这种和其他类型的客户端检测,我建议使用这个 js 库: http://hictech.github.io/navJs/tester/index.html

          对于您的具体答案,请使用:

          navJS.isIOS() || navJS.isAndroid()
          

          【讨论】:

            【解决方案10】:

            这是一个简单的 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>
            

            【讨论】:

              最近更新 更多