Simple Rails app, mostly scaffolding. I want to detect whether the user is using Android or iPhone to access my app. What's the easiest way to do that?
To detect the operating system on the client machine, one can simply use navigator. appVersion or navigator. userAgent property. The Navigator appVersion property is a read-only property and it returns a string which represents the version information of the browser.
Detect Operating System With the userAgent Property in JavaScript. If we want to track a little detail about the user's used browser and platform, we can go for the userAgent property with the navigator object. This special feature returns a string containing the browser's name, version, and platform name.
if request.env['HTTP_USER_AGENT'].downcase.match(/android|iphone/)
puts "yup, its mobile"
end
I know this question is old, but in case this helps someone else, I use this method in application_controller.rb
to automatically set the format to :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
The mobile?
method is separate so that you can also use it in your own controllers if you need to do some sort of conditional logic for mobile browsers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With