Skip to content

Instantly share code, notes, and snippets.

@kinopyo
Created March 27, 2012 14:28
Show Gist options
  • Save kinopyo/2216400 to your computer and use it in GitHub Desktop.
Save kinopyo/2216400 to your computer and use it in GitHub Desktop.
Rails: Detect if mobile agent
# from https://github.com/ruby-china/ruby-china/blob/master/app/helpers/application_helper.rb
MOBILE_USER_AGENTS = 'palm|blackberry|nokia|phone|midp|mobi|symbian|chtml|ericsson|minimo|' +
'audiovox|motorola|samsung|telit|upg1|windows ce|ucweb|astel|plucker|' +
'x320|x240|j2me|sgh|portable|sprint|docomo|kddi|softbank|android|mmp|' +
'pdxgw|netfront|xiino|vodafone|portalmmm|sagem|mot-|sie-|ipod|up\\.b|' +
'webos|amoi|novarra|cdm|alcatel|pocket|iphone|mobileexplorer|mobile'
def mobile?
agent_str = request.user_agent.to_s.downcase
return false if agent_str =~ /ipad/
agent_str =~ Regexp.new(MOBILE_USER_AGENTS)
end
@SylarRuby
Copy link

Could use an update as iPad now uses "macintosh" instead of "ipad". But what you have works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment