Verificar se o usuário é mobile em ASP.NET C#

segunda-feira, 15 agosto 2011 19:43 por leolima77

Classe que retorna "true" ou "false" caso o dispositivo esteja na lista de variáveis.

public class mobile : System.Web.UI.Page
    {
        private static string[] list = { "iphone", "nokia", "blackberry", "lg-", 
                                        "mot-", "palm", "samsung", "sec",
                                        "android", "and", "sonyericsson", "windows ce", "opera mini",
                                        "avantgo", "htc"
                                      };

        public bool isMobile()
        {
            bool bRetorno = false;
            string parameter = null;

            for (int i = 0; i < list.Length; i++)
            {
                HttpRequest Request = HttpContext.Current.Request;

                parameter = Request.UserAgent;

                if (!String.IsNullOrEmpty(parameter))
                {
                    if ((parameter.ToLower().IndexOf(list[i].ToLower()) > -1))
                    {
                       bRetorno = true;
                    }
                }
            }

            return bRetorno;
        }
    }
Tags:   , , , ,
Categorias:   Dicas
Ferramentas:   E-mail | del.icio.us | digg | rec6 | linkk | Permalink