2011-02-26 29 views

Respuesta

30
if request.env['HTTP_USER_AGENT'].downcase.match(/android|iphone/) 
    puts "yup, its mobile" 
end 
+7

Uso/android | iphone | ipod/i en mi expresión regular para capturar el iPod Touch. También le permite soltar el downcase (no distingue entre mayúsculas y minúsculas). – Callmeed

+0

¡Sorprendentemente respondida! lol – Hamdan

0
def getBrowser(bt) 
    rs=false 
    ua=request.env['HTTP_USER_AGENT'].downcase 
    isOpera = ua.index('opera') ? true : false 
    isSafari = (ua =~ /webkit|khtml/) ? true : false 
    isSafari3 = (ua.index('webkit/5') ? true : false 
    isGecko = (!isSafari and ua.index('gecko')) ? true : false 
    isGecko3 = (!isSafari and ua.index('rv:1.9')) ? true : false 
    isIE = (!isOpera and ua.index('msie')) ? true : false 
    isIE7 = (!isOpera and ua.index('msie 7')) ? true : false 
    case bt 
     when 0 #isKonqueror 
     if ua.index('konqueror') then rs=true end 
     when 1 #isOpera 
     rs=isOpera 
     when 2 #isSafari 
     rs=isSafari 
     when 3 #isSafari2 
     rs=isSafari && !isSafari3 
     when 4 #isSafari3 
     rs=isSafari3 
     when 5 #isIE 
     rs=isIE 
     when 6 #isIE6 
     rs=isIE && !isIE7 
     when 7 #isIE7 
     rs=isIE7 
     when 8 #isGecko 
     rs=isGecko 
     when 9 #isGecko2 
     rs=isGecko && !isGecko3 
     when 10 #isGecko3 
     rs=isGecko3 
     when 11 #isWindows 
     if ua.index('windows') or ua.index('win32') then rs=true end 
     when 12 #isMac 
     if ua.index('macintosh') or ua.index('mac os x') then rs=true 
end 
     when 13 #isAir 
     if ua.index('adobeair') then rs=true end 
     when 14 #isLinux 
     if ua.index('linux') then rs=true end 
     when 15 #isSecure 
     s = request.env['SERVER_PROTOCOL'].downcase 
     if s.index('https') then rs=true end 
    end 
    rs 
    end 

http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/8fa57719fd9316e1

9

Sé que esta pregunta es viejo, pero en caso de que esto ayude a alguien más, utilizo este método en application_controller.rb para ajustar automáticamente el formato de :mobile:

before_filter :detect_mobile 

protected 

def detect_mobile 
    request.format = :mobile if mobile? 
end 

def mobile? 
    request.user_agent =~ /iPhone|iPad|Android/i 
end 

El método mobile? está separado de modo que también puede usarlo en sus propios controladores si necesita hacer algún tipo de logi condicional c para navegadores móviles.

Cuestiones relacionadas