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?
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.