EN
JavaScript - detect Apple device (MacOS, iPhone, iPad, iPod)
10
points
In this short article, we would like to show how in Web Browser JavaScript, detect Apple device.
Quick solution:
// ONLINE-RUNNER:browser;
const expression = /(Mac|iPhone|iPod|iPad)/i;
if (expression.test(navigator.platform)) {
console.log('Apple device detected!');
} else {
console.log('Other device detected!');
}
Explaination
Apple uses the following platform names in navigator.platform:
Mac Computers:
------[ name ]------ -------[ description ]-------
Mac68K - Macintosh 68K system
MacPPC - Macintosh PowerPC system
MacIntel - Macintosh Intel system
Macintosh
iOS Devices:
------[ name ]------ -------[ description ]-------
iPhone - iPhone
iPad - iPad
iPod - iPod Touch
Simulators:
------[ name ]------ -------[ description ]-------
iPhone Simulator - simulator shipped with Xcode
iPad Simulator - simulator shipped with Xcode
iPod Simulator - simulator shipped with Xcode
Warning: Safari, Chrome and Mercury works well for the above platform names, but Opera may mess the above rules.