Check for Mobile iOS Device in JavaScript

- 1 minute read

Here’s how you can check whether someone on your website or app is using a mobile iOS device in JavaScript:

const ios = () => {
    if (typeof window === `undefined` || typeof navigator === `undefined`) return false;

    return /iPhone|iPad|iPod/i.test(navigator.userAgent || navigator.vendor || (window.opera && opera.toString() === `[object Opera]`));
};

A call to the ios() function will return true if the user is using an iPhone, iPad, or iPod. Otherwise, it will return false.

Link to this section Checking for iPhone usage

You could also check for a single device category. The below function will only return true for iPhone users:

const iphone = () => {
    if (typeof window === `undefined` || typeof navigator === `undefined`) return false;

    return /iPhone/i.test(navigator.userAgent || navigator.vendor || (window.opera && opera.toString() === `[object Opera]`));
};

Similarly, the iphone() function will return true if the user is using an iPhone, and it will otherwise return false.

Link to this section Conclusion

I’ve tried to make this snippet as precise as possible, but users can still fake their user agent header and so on.

Anyway, I hope you found this useful!