2011-02-26 12 views
11

सरल रेल ऐप, ज्यादातर मचान। मैं यह जानना चाहता हूं कि उपयोगकर्ता मेरे ऐप तक पहुंचने के लिए एंड्रॉइड या आईफोन का उपयोग कर रहा है या नहीं। ऐसा करने का सबसे आसान तरीका क्या है?रेल 3 में उपयोगकर्ता के ऑपरेटिंग सिस्टम का पता लगाने का सबसे आसान तरीका क्या है?

उत्तर

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

के लिए कुछ भी नहीं करता है मैं आईपॉड टच को पकड़ने के लिए अपने regex में/android | iphone | ipod/i का उपयोग करता हूं। यह आपको डाउनकेस छोड़ने देता है (मैं केस-असंवेदनशील हूं)। – Callmeed

+0

आश्चर्यजनक उत्तर दिया! जबरदस्त हंसी – 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

मैं जानता हूँ कि इस सवाल पुराना है, लेकिन इस मामले में यह किसी और में मदद करता है, मैं application_controller.rb में इस विधि का उपयोग स्वचालित रूप से :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 

mobile? विधि अलग है ताकि आप इसे अपने स्वयं के नियंत्रकों में भी उपयोग कर सकें यदि आपको किसी प्रकार की सशर्त लॉजी करने की आवश्यकता है मोबाइल ब्राउज़र के लिए सी।

0

उपयोगकर्ता_एजेंट मणि मोबाइल बनाम डेस्कटॉप ... और ब्राउज़र संस्करण के लिए उपयोगी है। लेकिन ओएस https://github.com/josh/useragent

संबंधित मुद्दे

 संबंधित मुद्दे