Rack middleware for mobile device detection
Pull request Compare This branch is 19 commits behind talison:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



sudo gem install rack-mobile-detect -s http://gemcutter.org


Rack::MobileDetect is Rack middleware for ruby webapps that detects mobile devices. It adds an X_MOBILE_DEVICE header to the request if a device is detected. The strategy for detecting a mobile device is as follows:

Targeted Detection

Search for a 'targeted' mobile device. A targeted mobile device is a device you may want to provide special content to because it has advanced capabilities - for example and iPhone or Android phone. Targeted mobile devices are detected via a Regexp applied against the HTTP User-Agent header.

By default, the targeted devices are iPhone, Android and iPod. If a targeted device is detected, the token match from the regular expression will be the value passed in the X_MOBILE_DEVICE header, i.e.: X_MOBILE_DEVICE: iPhone

UAProf Detection

Search for a UAProf device. More about UAProf detection can be found here.

If a UAProf device is detected, it will have X_MOBILE_DEVICE: true

Accept Header Detection

Look at the HTTP Accept header to see if the device accepts WAP content. More information about this form of detection is found here.

Any device detected using this method will have X_MOBILE_DEVICE: true

Catchall Detection

Use a 'catch-all' regex. The current catch-all regex was taken from the mobile-fu project

Any device detected using this method will have X_MOBILE_DEVICE: true


If none of the detection methods detect a mobile device, the X_MOBILE_DEVICE header will be absent.

Note that Rack::MobileDetect::X_HEADER holds the string 'X_MOBILE_DEVICE' that is inserted into the request headers.


use Rack::MobileDetect

This allows you to do mobile device detection with the defaults.

use Rack::MobileDetect, :targeted => /SCH-\w*$|[Bb]lack[Bb]erry\w*/

In this usage you can set the value of the regular expression used to target particular devices. This regular expression captures Blackberry and Samsung SCH-* model phones. For example, if a phone with the user-agent: 'BlackBerry9000/ Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/102' connects, the value of X_MOBILE_DEVICE will be set to 'BlackBerry9000'

use Rack::MobileDetect, :catchall => /mydevice/i

This allows you to limit the catchall expression to only the device list you choose.

    use Rack::MobileDetect, :redirect_to => '/mobile'

This allows you to choose a custom redirect path


A Sinatra app called echo_env.rb is available in the util/ directory. Hit this app with a mobile device to see the various HTTP headers and whether or not the X_MOBILE_DEVICE header is added by Rack::MobileDetect.

See the unit test source code for more info.