Detect iPad 3 in Ruby

点点圈 提交于 2019-12-08 05:40:05

问题


Is it possible to detect an iPad 3 (aka The new iPad) using ruby/rails? If so, how would I go about doing it?


回答1:


You can detect the device but I do not think it is possible to detect device version. As far as I know, Apple provides a user agent string which appears as follows:

Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 
(KHTML, like   Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10

There is no machine version information in this string and I do not know any other way to detect user machine.

If you do not know how to get user agent string in Rails, check here.




回答2:


Ordinarily, RoR doesn't know anything about the browser's viewport dimensions or the resolution of the machine on which the browser is running. However, you can read these in using JavaScript and pass them to the server in a hidden input. (See here.)

For the moment--that's an important caveat--the iPad 3 is the only iPad with a resolution of 2048 x 1536. Your script would be

(
    (screen.width == 2048 && screen.height == 1536) || 
    (screen.width == 1536 && screen.height == 2048)
) 

which accounts for the iPad 3's possible resolution in both landscape and portrait modes.

request.env['HTTP_USER_AGENT'].match(/iPad/i) will tell you whether the requesting device is an iPad.

Hit on both and it's an iPad 3. This isn't entirely satisfactory, but it works.




回答3:


You can detect whether the device is an iPad by looking at the user agent string, you can detected whether the device has a retina display by checking whether window.devicePixelRatio is 2 (using client-side JavaScript). The device is an iPad3 if iPad is in the user agent string and the pixel ratio is 2.

You can pass the devicePixelRatio by:

  • passing it to the server as a cookie
  • dynamically appending it to urls in your page with JavaScript
  • dynamically putting it in a hidden field in a form

You may have to redirect (Using window.location) the user back to the current page if the cookie is not set.



来源:https://stackoverflow.com/questions/9755323/detect-ipad-3-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!