4

I wanted to find out all the version's of iPad (excluding iPhone, iPod) from user agent string, currently while testing I got the following string

Mozilla/5.0 (iPad; CPU OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3

Question: Will the following code works fine for all the iPad's?

String userAgentStr = request.getHeader("User-agent");
if (userAgentStr.contains("iPad"))
{
    //do my logic
}

EDIT:

I am using Dolphin browser from iPad but I am getting the following UA string:

Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en_US) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8C148 Safari/6533.18.5

Similarly iBrowser from iPad UA string is:

Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B206

So my above code is breaking...

Mahendran
  • 2,191
  • 5
  • 24
  • 40
  • 1
    Maybe, maybe not, maybe for now but not in the future … user agent strings are notorious for non-X to start claiming to be X because it is (to some degree) X-compatible (that user agent string doesn't come from Mozilla, OS X or Gecko, but string matching the UA would tell you it was). Don't use the UA. Use feature detection. – Quentin Dec 03 '12 at 12:08
  • @Quentin How to use feature detection? – Mahendran Dec 03 '12 at 12:23
  • It depends on what feature you wish to detect. – Quentin Dec 03 '12 at 12:31
  • https://www.google.com/search?q=javascript+feature+detection – Aprillion Dec 03 '12 at 12:36
  • Why do you want to do that? There is no `contains` method for javascript string instances. – RobG Dec 03 '12 at 13:28
  • possible duplicate of [What is the iPad user agent?](http://stackoverflow.com/questions/2153877/what-is-the-ipad-user-agent) – Peter O. Dec 13 '12 at 07:20

1 Answers1

1

I strongly recommend using feature detection instead of device detection as suggested in the comments. One library commonly used for feature detection would be modernizr.

If you have to use device detection, this gist basically says that your code should work.

davidpfahler
  • 606
  • 4
  • 7