Tuesday, June 22, 2010

detect request is from iPhone in ASP.NET

The usage of mobiles is growing and growing day to day and most of the users are browsing from their mobiles and do the stuff. So, it is becoming important to support the mobile phones the web sites we are developing. Recently we have developed applications which support for iPhone and I am strongly believing that ASP.NET MVC is the best architecture in all ASP.NET versions.

As we are developing applications it can be accessible from any where and any device including computers, mobiles, iphone, ipod or whatever. But, the important thing is UI. A web site how it looks like on computers is completely different from the mobile. So, we need to detect the user agent or the client who requested the site. So, if  request from computer browsers then we don't need to anything as this is the default type. Whereas the request from the mobile phones then we need to render different UI. So, this post will explains that.

Detect request from iPhone in ASP.NET [C#]:
if(HttpContext.Current.Request.UserAgent.ToLower.Contains("iphone"))
{
     //iPhone is the requested client. Write logic here.
}
else
{
     //Normal browsers [from computers] requested.
}

Detect the iPhone request in Java script:
if (navigator.userAgent.match(/iPhone/i)
So, depends on the client we will write the corresponding logic in the specific block to switch UI. Hope this helps.

No comments:

Post a Comment