How do you recognize Mobile Browser??


Hello Developers,

You all might have noticed when you browse any popular sites from any mobile device, your browser gives you a mobile compatible version of the same sites not the site you see on the desktop or laptop..Right!!!!

HOW DO THEY DO THAT…

Answer is very simple…They recognize the “User Agent” and do the tweaking.

User Agent holds the description about the browser, operting system, browser version, OS version etc. We can easily get this from the request header.

For example:

Mozilla/5.0 (Linux; U; Android 2.2.1; de-de; X2 Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

This example shows the request has come from an Android device. Similarly there are different USER AGENTS for different devices. You can easily get this by using Javascript or Code Behind.

Javascript:  Using Navigator object. As

navigator.userAgent

ASP.NET CODE BEHIND: Using Request object

Request.ServerVariables[“http_user_agent”];

Now you can write a static helper method to recognize the requested device as below

public static bool IsMobileDevice(string userAgent)   {

userAgent = userAgent.ToLower();

return userAgent.Contains(“iphone”) |

userAgent.Contains(“android”) |

userAgent.Contains(“blackberry”) |

userAgent.Contains(“palm”);

}

Similarly you can have a Dictionary or List of devices and get the peroper device through these objects.

Thanks for reading this article.

With Personal Regards

Swagat