检测iOS / Android操作系统 [英] Detecting iOS / Android Operating system

查看:147
本文介绍了检测iOS / Android操作系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've done some research, and this question has come up, but not in the way I intend. I'm building a page for a client that is a QR code landing, which is a place to download an application. So he doesn't have to print out 2 QR codes on a page, I'd like to detect the current operating system (Apple/Android/Other[not supported]) and modify my elements based on that value.

我看过脚本detectmobilebrowsers,这只是为了告诉用户是否完全是移动用户,而我想知道用户的操作系统是什么正在运行并建议最好的应用程序版本。

I've looked at the script "detectmobilebrowsers" and that is just aimed at telling whether or not the user is mobile at all, whereas I would like to figure out what operating system the user is running and suggest the best application version.

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

Other answers I found similar to this question seemed either outdated or unreliable (has no detection for Android tablet browsers), so I'm in search of something new. How can I achieve this? (Preferably using jQuery - Javascript - PHP in that order).

推荐答案

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

You can test the user agent string:

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

这篇关于检测iOS / Android操作系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆