mobile browser device detection in .NET [closed]

不想你离开。 提交于 2019-11-30 17:37:02

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.

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.

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)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!