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

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

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


当前回答

使用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);

其他回答

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

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

/**
 * 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";
}

这个问题已经在这里解决了:在jQuery中检测移动设备的最佳方法是什么?

对于已接受的答案,它们基本上会测试它是否是iPhone, iPod, Android设备或其他设备,以返回true。只保留那些你想要的,例如if(/Android/i.t test(navigator.userAgent)){//一些代码..}将只对Android用户代理返回true。

然而,用户代理并不是真正可靠的,因为它们可以被更改。最好的事情仍然是开发适用于所有手机平台的东西。

您还可以创建Firbase动态链接,这将根据您的需求工作。它支持多种平台。该链接可以手动创建,也可以通过编程创建。你可以把这个链接嵌入到二维码中。

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

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

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