Languages
[Edit]
EN

JavaScript - detect Apple device (MacOS, iPhone, iPad, iPod)

10 points
Created by:
Saim-Mccullough
718

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.

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join