iPhone 'user agent' FAQ: I'm trying to optimize my HTML/web app for the iPhone; how do I detect an iPhone, iPod, or iPad browser in my web application?
Here's what the iPhone user agent string looks like for iOS 2:
Mozilla/5.0 (iPhone; U; CPU iOS 2_0 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/XXXXX Safari/525.20
That's actually one long string, but I wrapped it here to reduce scrolling.
var isIphone = navigator.userAgent.indexOf("iPhone") != -1 ;
That statement makes the variable isIphone true if the string "iPhone" is seen in the browser user agent text, or false if the text is not found.
You can use just that one line if you're really only concerned about detecting the iPhone in your HTML application, or you can also perform other checks for the iPod and iPad to detect all iOS devices:
var isIphone = navigator.userAgent.indexOf("iPhone") != -1 ; var isIod = navigator.userAgent.indexOf("iPod") != -1 ; var isIpad = navigator.userAgent.indexOf("iPad") != -1 ; // now set one variable for all iOS devices var isIos = isIphone || isIpod || isIpad ;