a lightweight mobile device dedection with .net c#

by nolovelust 19. February 2010 15:42

This simple function will return true if browser is a mobile browser

public bool IsMobile()
{


try //try , because sometimes user agent is empty


{
string ua = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"].ToString().ToLower();
string[] uaArray = { "android", "maui", "ppc", "opera mobi", "symbian", "series", "nokia", "mot-", "motorola", "lg-", "lge", "nec-", "lg/", "samsung", "sie-", "sec-", "sgh-", "sonyericsson", "sharp", "windows ce", "portalmmm", "o2-", "docomo", "philips", "panasonic", "sagem", "smartphone", "up.browser", "up.link", "googlebot-mobile", "googlebot-image", "slurp", "spring", "alcatel", "sendo", "blackberry", "opera mini", "opera 2", "netfront", "mobilephone mm", "vodafone", "avantgo", "palmsource", "siemens", "toshiba", "i-mobile", "asus", "ice", "kwc", "htc", "softbank", "playstation", "nitro", "iphone", "ipod", "google wireless transcoder", "t-mobile", "obigo", "brew" };
bool foundInArray = false;
foreach (string s in uaArray)
{
if (ua.IndexOf(s) > -1)
{
foundInArray = true;
break;
}
}

if (foundInArray==true || HttpContext.Current.Request.Browser.IsMobileDevice || HttpContext.Current.Request.ServerVariables["HTTP_ACCEPT"].Contains("application/vnd.wap.xhtml+xml"))
{
return true;
}
else
{
return false;
}
}
catch (Exception e)
{
return false;
}
}

Tags:

ASP.NET | Mobile web | Open Source

Add comment

  Country flag

biuquote
Loading

Adverts

Welcome

Tag cloud

Month List