http://our.umbraco.org/forum/templating/templates-and-document-types/18616-Detecting-mobile-and-tabletsDetecting mobile and tablets
Carlos started this topic 10 months ago , this topic was edited at: Saturday, April 30, 2011 8:05 AM
So I wrote a post a while back on how we detected mobile devices and tablets and redirected them to different templates defined by the author. HERE IS THE LINK.
HOWEVER, my inclination about detecting Android tablets was wrong. We were using in template C# in our Global template. We didn't want to use 51Degrees.mobi since it would not fit our needs. However, being in contact with the person over at 51Degrees.mobi, he is planning on working with the Umbraco people to help integrate 51degrees into Umbraco more gracefully.
Ok Android tablet detection.
We were using string.Contains("Android") this got everything that was containing the user agent "Android". This didn't work in differentiating between mobile and tablet. My mistake. So my work around was this huge client site JS solution that kind of work part of the time.
Simple solution I did today. strUserAgent.Contains("Android") && !strUsrAgent.Contains("mobile"). Found out HERE that recommended by the Android dev team to no include "mobile" if you want your tablet to redirect to a mobile template. Or if you want to redirect to a tablet specific or desktop version of your site.
The solution below should cover all Android tablets so you don't have to look for specific devices.
Here is our full solution. Remember, we put this in our Global template in the <head> section.
<script runat="server">
protected string outof = "";
protected void Page_Load(object sender, EventArgs e)
{
string strUserAgent = Request.UserAgent.ToLower();
if (strUserAgent != null)
{
if (
Request.Browser.IsMobileDevice == true
|| strUserAgent.Contains("iphone")
|| strUserAgent.Contains("ipod")
|| strUserAgent.Contains("android")
|| strUserAgent.Contains("iemobile")
//|| strUserAgent.Contains("version")
//|| strUserAgent.Contains("blackberry")
//|| strUserAgent.Contains("windows ce")
//|| strUserAgent.Contains("opera mini")
//|| strUserAgent.Contains("palm")
//|| strUserAgent.Contains("chrome")
|| strUserAgent.Contains("ipad")
)
{
//To detect iPad and Android tablet.
if (strUserAgent.Contains("ipad") || (strUserAgent.Contains("android") && !strUserAgent.Contains("mobile")))
{
if (umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("chooseTabletTemplate") != null)
{
Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri + "/" + umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("chooseTabletTemplate").Value, true);
}
}
else
{
if (umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("chooseMobileTemplate") != null)
{
Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri + "/" + umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("chooseMobileTemplate").Value, true);
}
}
}
}
}
</script>