Testing for mobile browser
I've been looking for a .net version of some code to test to see if the user is viewing the site with a mobile browser. Here's what I came up with cobbled together from several references:
public bool isMobileBrowser()
{
string strMKey = null;
string strHttpAccept = null;
string strHttpUserAgent = null;
strHttpAccept = Request.ServerVariables["HTTP_ACCEPT"].ToLower();
strHttpUserAgent = Request.ServerVariables["HTTP_USER_AGENT"].ToLower();
// User Agent is windows but not windows mobile?
if ((strHttpUserAgent.IndexOf("windows") >= 0) && !(strHttpUserAgent.IndexOf("windows ce") >= 0))
{
return false;
}
// User agent matches one of these keywords?
string[] Key = new string[] { "up.browser", "up.link", "windows ce", "iemobile", "mini", "mmp", "symbian", "midp", "wap", "phone", "pocket", "mobile", "pda", "psp", "iphone" };
for (int i = 0; i < Key.Length; i++)
{
if (strHttpUserAgent.IndexOf(Key[i]) >= 0)
{
return true;
}
}
if ((strHttpAccept.IndexOf("text/vnd.wap.wml") >= 0) || (strHttpAccept.IndexOf("application/vnd.wap.xhtml+xml") >= 0))
{
return true;
}
if ((Request.ServerVariables["HTTP_X_WAP_PROFILE"] != null) && (Request.ServerVariables["HTTP_X_WAP_PROFILE"].ToString() != ""))
{
return true;
}
if ((Request.ServerVariables["HTTP_PROFILE"] != null) && (Request.ServerVariables["HTTP_PROFILE"].ToString() != ""))
{
return true;
}
if ((Request.ServerVariables["X-OperaMini-Features"] != null) && (Request.ServerVariables["X-OperaMini-Features"].ToString() != ""))
{
return true;
}
if ((Request.ServerVariables["UA-pixels"] != null) && (Request.ServerVariables["UA-pixels"].ToString() != ""))
{
return true;
}
// Check first four char from strHttpUserAgent
string[] M = new string[] { "acs-", "alav", "alca", "amoi", "audi", "aste", "avan", "benq", "bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs", "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-", "newt", "noki", "opwv", "palm", "pana", "pant", "pdxg", "phil", "play", "pluc", "port", "prox", "qtek", "qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem", "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-", "tosh", "treo", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", "winw", "xda-", "modu" };
strMKey = strHttpUserAgent.Substring(0, 4).Replace(" ", "-");
for (int i = 0; i <M.Length; i++)
{
if (M[i] == strMKey)
{
return true;
}
}
return false;
}
public bool isMobileBrowser()
{
string strMKey = null;
string strHttpAccept = null;
string strHttpUserAgent = null;
strHttpAccept = Request.ServerVariables["HTTP_ACCEPT"].ToLower();
strHttpUserAgent = Request.ServerVariables["HTTP_USER_AGENT"].ToLower();
// User Agent is windows but not windows mobile?
if ((strHttpUserAgent.IndexOf("windows") >= 0) && !(strHttpUserAgent.IndexOf("windows ce") >= 0))
{
return false;
}
// User agent matches one of these keywords?
string[] Key = new string[] { "up.browser", "up.link", "windows ce", "iemobile", "mini", "mmp", "symbian", "midp", "wap", "phone", "pocket", "mobile", "pda", "psp", "iphone" };
for (int i = 0; i < Key.Length; i++)
{
if (strHttpUserAgent.IndexOf(Key[i]) >= 0)
{
return true;
}
}
if ((strHttpAccept.IndexOf("text/vnd.wap.wml") >= 0) || (strHttpAccept.IndexOf("application/vnd.wap.xhtml+xml") >= 0))
{
return true;
}
if ((Request.ServerVariables["HTTP_X_WAP_PROFILE"] != null) && (Request.ServerVariables["HTTP_X_WAP_PROFILE"].ToString() != ""))
{
return true;
}
if ((Request.ServerVariables["HTTP_PROFILE"] != null) && (Request.ServerVariables["HTTP_PROFILE"].ToString() != ""))
{
return true;
}
if ((Request.ServerVariables["X-OperaMini-Features"] != null) && (Request.ServerVariables["X-OperaMini-Features"].ToString() != ""))
{
return true;
}
if ((Request.ServerVariables["UA-pixels"] != null) && (Request.ServerVariables["UA-pixels"].ToString() != ""))
{
return true;
}
// Check first four char from strHttpUserAgent
string[] M = new string[] { "acs-", "alav", "alca", "amoi", "audi", "aste", "avan", "benq", "bird", "blac", "blaz", "brew", "cell", "cldc", "cmd-", "dang", "doco", "eric", "hipt", "inno", "ipaq", "java", "jigs", "kddi", "keji", "leno", "lg-c", "lg-d", "lg-g", "lge-", "maui", "maxo", "midp", "mits", "mmef", "mobi", "mot-", "moto", "mwbp", "nec-", "newt", "noki", "opwv", "palm", "pana", "pant", "pdxg", "phil", "play", "pluc", "port", "prox", "qtek", "qwap", "sage", "sams", "sany", "sch-", "sec-", "send", "seri", "sgh-", "shar", "sie-", "siem", "smal", "smar", "sony", "sph-", "symb", "t-mo", "teli", "tim-", "tosh", "treo", "tsm-", "upg1", "upsi", "vk-v", "voda", "wap-", "wapa", "wapi", "wapp", "wapr", "webc", "winw", "xda-", "modu" };
strMKey = strHttpUserAgent.Substring(0, 4).Replace(" ", "-");
for (int i = 0; i <M.Length; i++)
{
if (M[i] == strMKey)
{
return true;
}
}
return false;
}

1 Comments:
you should add "ipod" to your user-agent strings to get iPod Touch users.
Post a Comment
Subscribe to Post Comments [Atom]
<< Home