.NET 中的移动浏览器设备检测

发布于 2024-11-30 03:26:53 字数 172 浏览 0 评论 0原文

我刚刚开始创建我的第一个用 WebForms 编写的桌面网站的移动版本。

我当前的问题与移动设备/浏览器检测有关。

我想要确定的是a)您的设备是否是移动设备b)什么操作系统(Android / IOS /等)以防我需要根据操作系统以不同的方式处理任何事情以及c)什么屏幕尺寸(用于加载不同的样式表)

I'm just starting to create my first mobile version of a desktop website that was written in WebForms.

My current question has to do with mobile device/browser detection.

What I'm trying to determine is a) If your device is mobile b) What OS (Android/IOS/etc) in case I need to handle anything differently based on the OS and c) What screen size (for loading different stylesheets)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦与时光遇 2024-12-07 03:26:53

检测浏览器类型最简单的方法是查看 useragent 字符串。该字符串中的关键字将有助于检测浏览器。 UserAgentString.com 维护着用户代理字符串的完整列表,但您需要查看的主要内容for 只是几个关键字。

例如,“blackberry”一词仅在从 Blackberry 设备浏览时才会出现。与 iPad 和 iPhone 类似。 Android 设备都在 useragent 字符串中显示“android”,但它们通过包含手机关键字“mobile”来区分平板电脑和手机。

以下是我们在移动应用程序中检测台式机、手机和平板电脑的方法:

    public enum DeviceType
    {
        Desktop,
        Tablet,
        Phone
    }

    public static DeviceType UserAgentToDeviceType(string userAgent)
    {
        if (userAgent.ToLowerInvariant().Contains("blackberry"))
            return DeviceType.Phone;

        if (userAgent.ToLowerInvariant().Contains("iphone"))
            return DeviceType.Phone;

        if (userAgent.ToLowerInvariant().Contains("ipad"))
            return DeviceType.Tablet;

        if (userAgent.ToLowerInvariant().Contains("android"))
        {
            if (userAgent.ToLowerInvariant().Contains("mobile"))
                return DeviceType.Phone;
            else
                return DeviceType.Tablet;
        }

        return DeviceType.Desktop;
    }

如果您使用的是 jQuery Mobile 之类的东西,无论设备类型如何,网站都会针对移动外观进行定制,并且它将处理不同设备上 JavaScript 引擎之间的差异。

Detecting the type of browser is simplest by looking at the useragent string. Key words in that string will help detect the browser. UserAgentString.com maintains a thorough list of useragent strings, but the main thing you need to look for are only a few keywords.

For example, the word "blackberry" only ever shows up when browsing from a Blackberry device. Similar with iPad and iPhone. Android devices all show "android" in the useragent string, but they distinguish between tablets and phones by inclusion of the keyword "mobile" for phones.

Here's how we detect Desktops, Phones, and Tablets in our mobile application:

    public enum DeviceType
    {
        Desktop,
        Tablet,
        Phone
    }

    public static DeviceType UserAgentToDeviceType(string userAgent)
    {
        if (userAgent.ToLowerInvariant().Contains("blackberry"))
            return DeviceType.Phone;

        if (userAgent.ToLowerInvariant().Contains("iphone"))
            return DeviceType.Phone;

        if (userAgent.ToLowerInvariant().Contains("ipad"))
            return DeviceType.Tablet;

        if (userAgent.ToLowerInvariant().Contains("android"))
        {
            if (userAgent.ToLowerInvariant().Contains("mobile"))
                return DeviceType.Phone;
            else
                return DeviceType.Tablet;
        }

        return DeviceType.Desktop;
    }

If you are using something like jQuery Mobile, the site will be customized for mobile appearance regardless of device type, and it will handle differences between the JavaScript engine on different devices.

罪#恶を代价 2024-12-07 03:26:53

在许多情况下,我不一定认为我提出的解决方案是最佳解决方案,但它可能会证明您对问题领域的另一种见解。

而不是检测移动浏览器本身,它与浏览器嗅探有一些相似之处/缺点。

相反,采用响应式设计的方法。我不会在这里详细讨论响应式设计,因为它会让我们偏离正轨。然而,它可以为您提供一种方法,它不是根据检测到的浏览器来定制整个体验,而是一种根据屏幕分辨率、CSS 功能、启用的 JavaScript 等定制体验的更微妙的方法。

响应式设计不是它本身并不是一种技术,而是一组能够根据所使用的浏览器(移动浏览器)逐步增强体验的技术。

响应式技术实际上不允许(或者至少受到损害)的是移动版本/桌面版本之间的巨大差异。由于每种技术通常都会污染单独的体验,例如 html 可以隐藏在移动版本上,但可能仍然在后台下载......但这些技术正在开发,例如 JavaScript 可用于在移动浏览器上下载低分辨率图像,并且宽屏幕显示器上的高分辨率。

但您始终可以放置一个指向完全独立的移动版本/桌面版本网站的链接,以允许用户决定作为后备。

I do not necessarily think what I am proposing is the best solution in many cases, however it may prove an alternative insight into your problem area.

Instead of a detecting a mobile browser per se, which has some similarities / disadvantages to browser sniffing.

Instead take the approach of responsive design. I won't go into detail of responsive design here, as it's taking us off track. However what it could provide you with is an approach which instead of customising the entire experience based upon what browser is detected, it's a subtler method of customising the experience based upon screen resolutions, css capabilities, JavaScript being enabled etc etc.

Responsive design isn't a technology per-say, but a set of techniques that enable the experience to be progressively enhanced depending on the browser (mobile browser) being used.

What a responsive technique doesn't really allow for (or at least it is compromised) is very dramatic differences between e.g. mobile version / desktop version. As each would typically pollute the separate experience e.g. html could be hidden on a mobile version, but might be still downloaded in the background... but these techniques are developing e.g. JavaScript can be used to download a low resolution image on a mobile browser and a high resolution on a wide screen monitor.

But you could always put a link to a completely separate mobile version / desktop version of site to allow the user to decide as a fall-back.

终遇你 2024-12-07 03:26:53

WURFL 是这方面的一个很好的资源。您可以配置所需的值(无需获取其所有参数)。我认为如果您想创建自己的解决方案 -

1)您可以采用 WURFL XML
2)根据您的需要列出论点
3) 使用 XML Reader 读取其 xml 并将值放入数据库中。

这样你就可以有自己的解决方案。 (尽管有一个问题是稍后在 WURFL 上注册的新设备不会自动更新,因此您必须稍后自行更新)

WURFL is a great resource for this. You can configure what values you need (no need to take all of its arguments). I think if you want to create your own solution-

1) You can take WURFL XML
2) Shortlist arguments as per your needs
3) Use XML Reader to read its xml and put values in your database.

This way you can have your own solution. (aLthough one issue is that new devices that register later on WURFL will not be updated automatically, so you have to later on update it yourself)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文