iPad isn't recognized as iOS device in helper

We have the following helper to recognize when a user uses an iOS device.

Whilst it’s working for an iPhone, my iPad (couple years old) isn’t recognized by the helper and is instead put under the macOS category which is wrong.

Here’s the isMacOs code:

const isMacOs = function () {
    const { userAgent } = window.navigator;
    return /mac/gi.test(userAgent);
};

Here is the isIOS helper which should trigger for my iPad (but doesn’t):

const isIOS = function () {
    const { userAgent } = window.navigator;
    return /iPad|iPhone|iPod/.test(userAgent) && !window.MSStream;
};

Can anyone point me to the error why it’s not working and maybe how you have implemented this?

Thanks in advance,

Andreas

According to Chrome Developer Tools the user agent for “Safari – iPad OS 13.2” is as follows:

Mozilla/5.0 (iPad; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1

…meaning your code should be just right, unless it’s the condition !window.MSStream that’s causing the problem.

Furthermore, this source uses navigator.platform for the checking rather than window.navigator:

function isIOSDevice(){
   return !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform);
}

I used Chrome from my iPad, so that must be the problem.

Will give this a try instead, makes more sense.

Thanks Peter!

1 Like