Apple Agents
Devices which request content from your website (usually) pass a user agent string. This contains information such as its name, OS, browser version, and rendering engine. Apple’s gadgets pass the following user agents, although you may find subtle variations in the version numbers: iPhone:
Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3
iPod:
Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3A101a Safari/419.3
iPad:
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/7B367 Safari/531.21.10
Ascertaining Apple
The user agent string can be analyzed on the server or client to determine which device is accessing your site. That said, remember browser sniffing stinks: it’s better to create a single website which works on a variety of browsers than maintain multiple differing versions. However, you could use this code to collate statistics or create alternative view of your web application which matches the iPhone interface. Without further ado, here’s some JavaScript detection code:
// Apple detection object
var Apple = {};
Apple.UA = navigator.userAgent;
Apple.Device = false;
Apple.Types = ["iPhone", "iPod", "iPad"];
for (var d = 0; d < Apple.Types.length; d++) {
var t = Apple.Types[d];
Apple[t] = !!Apple.UA.match(new RegExp(t, "i"));
Apple.Device = Apple.Device || Apple[t];
}
// is this an Apple device?
alert(
"Apple device? " + Apple.Device +
"niPhone? " + Apple.iPhone +
"niPod? " + Apple.iPod +
"niPad? " + Apple.iPad
);
Similar code can be developed in PHP for server-side detection:
// Apple detection array
$Apple = array();
$Apple['UA'] = $_SERVER['HTTP_USER_AGENT'];
$Apple['Device'] = false;
$Apple['Types'] = array('iPhone', 'iPod', 'iPad');
foreach ($Apple['Types'] as $d => $t) {
$Apple[$t] = (strpos($Apple['UA'], $t) !== false);
$Apple['Device'] |= $Apple[$t];
}
// is this an Apple device?
echo
"<p>Apple device? ", ($Apple['Device'] ? 'true' : 'false'),
"</p>n<p>iPhone? ", ($Apple['iPhone'] ? 'true' : 'false'),
"</p>n<p>iPod? ", ($Apple['iPod'] ? 'true' : 'false'),
"</p>n<p>iPad? ", ($Apple['iPad'] ? 'true' : 'false'),
'</p>';
Alternatively, you could use Apache’s .htaccess file to redirect to an Apple-specific version of your website on another URL or sub-domain, e.g.
RewriteCond %{HTTP_USER_AGENT} ^.*(iP.*$
RewriteRule ^(.*)$ http://apple.site.com [R=301]
Useful stuff, but an important question remains: should we be developing separate versions of our web sites for these popular devices?
Frequently Asked Questions (FAQs) about Identifying Apple iPhone, iPod, and iPad Visitors
How can I identify the type of Apple device visiting my website?
You can identify the type of Apple device visiting your website by examining the user agent string that the device sends to your server. This string contains information about the device’s operating system, browser, and model. For example, an iPhone 7 would have a user agent string that includes “iPhone7,2”. You can use this information to tailor your website’s content to the specific device.
What is a user agent string?
A user agent string is a line of text that a web browser sends to a server to identify itself. It typically includes information about the browser’s name, version, and the operating system it’s running on. For example, the user agent string for an iPhone running Safari might look like this: “Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1”.
How can I use the user agent string to improve my website?
By analyzing the user agent strings of your website visitors, you can gain insights into the types of devices they are using. This can help you optimize your website for those devices. For example, if you find that a large number of your visitors are using iPhones, you might want to ensure that your website is fully optimized for mobile viewing.
What does the number after “iPhone” in the user agent string mean?
The number after “iPhone” in the user agent string refers to the model of the iPhone. For example, “iPhone7,2” refers to an iPhone 6. This can be useful information for understanding the capabilities of the device, such as its screen resolution and processing power.
How can I find the user agent string for my own device?
You can find the user agent string for your own device by visiting a website that displays it. There are many such websites available, simply search for “what is my user agent” in your web browser.
Can the user agent string be faked?
Yes, it is possible for a user agent string to be faked. Some web browsers allow users to change their user agent string, and there are also tools available that can do this. However, most users do not change their user agent string, so it is still a useful tool for identifying the types of devices visiting your website.
What is the difference between an iPhone and an iPod user agent string?
The main difference between an iPhone and an iPod user agent string is the device identifier. An iPhone user agent string will include “iPhone”, while an iPod user agent string will include “iPod”. The rest of the user agent string will be similar, as both devices use the same operating system and web browser.
How can I use the user agent string to detect an iPad?
You can detect an iPad by looking for “iPad” in the user agent string. This will be present regardless of the model of the iPad.
Can I use the user agent string to detect the version of iOS running on a device?
Yes, the user agent string includes information about the version of iOS running on the device. This is represented by the numbers after “CPU iPhone OS” or “CPU OS”. For example, “CPU iPhone OS 10_3_1” indicates that the device is running iOS 10.3.1.
What other information can I get from the user agent string?
In addition to the device type and operating system version, the user agent string also includes information about the web browser being used, including its name and version. This can be useful for ensuring that your website is compatible with the browsers that your visitors are using.
Craig is a freelance UK web consultant who built his first page for IE2.0 in 1995. Since that time he's been advocating standards, accessibility, and best-practice HTML5 techniques. He's created enterprise specifications, websites and online applications for companies and organisations including the UK Parliament, the European Parliament, the Department of Energy & Climate Change, Microsoft, and more. He's written more than 1,000 articles for SitePoint and you can find him @craigbuckler.