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

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

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


当前回答

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

对于你的具体答案使用:

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

其他回答

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

对于你的具体答案使用:

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

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

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

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

你也可以在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();

}