Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect iPad Pro as iPad using javascript?

We were able to detect an iPad device using javascript like this:

function isDeviceiPad(){
    return navigator.platform.match(/iPad/i);
}

That worked perfectly in detecting iPad devices, but when we checked from an iPad Pro (10.5 inch), it does not detect that it is an iPad.

To further investigate, we drilled down into the navigator object, checked both platform and userAgent, and got these results:

navigator.platform = 'MacIntel';
navigator.userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) 
 AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15)';

The issue is that navigator.platform = 'MacIntel' (which is the same as the MacBook Pro) is returned instead of the iPad. We need a way to detect that this is an iPad and not a MacBook Pro, but it seems that the navigator does not return iPad like it does with older iPads.

Any idea how we can fix this issue?

like image 241
Wonka Avatar asked Sep 03 '19 17:09

Wonka


People also ask

How can I tell if my iPhone is JavaScript or iPad?

var isIOS = (function () { var iosQuirkPresent = function () { var audio = new Audio(); audio. volume = 0.5; return audio. volume === 1; // volume cannot be changed from "1" on iOS 12 and below }; var isIOS = /iPad|iPhone|iPod/. test(navigator.

Does iPad Pro have JavaScript?

On the Advanced settings for Safari screen, you'll see a few options. Near the middle is the option for enabling or disabling JavaScript for Safari on your iPhone or iPad. To enable JavaScript, slide the switch to the right.


7 Answers

iPadPro reports navigator.platform the browser as 'MacIntel', but that is the same as other platforms.

Currently (2019) difference between iPadPro and the other platforms is that iPadPro is touch enabled.

Here are a couple of helpful methods.

function isIOS() {
  if (/iPad|iPhone|iPod/.test(navigator.platform)) {
    return true;
  } else {
    return navigator.maxTouchPoints &&
      navigator.maxTouchPoints > 2 &&
      /MacIntel/.test(navigator.platform);
  }
}

function isIpadOS() {
  return navigator.maxTouchPoints &&
    navigator.maxTouchPoints > 2 &&
    /MacIntel/.test(navigator.platform);
}

like image 130
Steven Spungin Avatar answered Oct 17 '22 04:10

Steven Spungin


I guess that iPad Pro is upgraded to iPadOS 13 Beta. Since Apple claimed Desktop-Class Browsing with Safari on iPadOS, it seems mobile Safari also mimics macOS behavior and user agent.

So, the short answer is it's not possible.

However you can try workarounds from answers to this question.

like image 40
Eugene Berdnikov Avatar answered Oct 17 '22 02:10

Eugene Berdnikov


You can use Regular Expression for this.

var isIPadPro = /Macintosh/.test(navigator.userAgent) && 'ontouchend' in document;
like image 9
Lesha Petrov Avatar answered Oct 17 '22 04:10

Lesha Petrov


Currently, in October 2020 the only way I know is:

(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 0) || navigator.platform === 'iPad'
like image 9
bojo Avatar answered Oct 17 '22 04:10

bojo


You may use screen size to check it, iPad pro came with 2 different side. Detail implement bellow, modify it as your use case

function isIpadPro() {
    var ratio = window.devicePixelRatio || 1;
    var screen = {
        width : window.screen.width * ratio,
        height : window.screen.height * ratio
    };
    return (screen.width === 2048 && screen.height === 2732) || (screen.width === 2732 && screen.height === 2048) || (screen.width === 1536 && screen.height === 2048) || (screen.width === 2048 && screen.height === 1536);
}

Screen size reference: http://screensiz.es/

like image 4
Vo Kim Nguyen Avatar answered Oct 17 '22 04:10

Vo Kim Nguyen


The most easiest way to detect an "iPad Pro 10.5" device is through checking its screen size which is "window.screen.height x window.screen.width = 1112 x 834"

However, I am wondering why you need to detect the device model. In case you want to detect mobile browsers, take a look at this question: Detecting Mobile Browser

like image 2
Reza Avatar answered Oct 17 '22 03:10

Reza


You should be able to use the screen size to distinguish them. Thought you will need to find the real value for each iPad pro you want to detect.

window.screen.height
window.screen.width
like image 1
Jonatan Cloutier Avatar answered Oct 17 '22 03:10

Jonatan Cloutier