Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinguish between iPad and mac on iPad with iPadOs

In iOS 13 apple changed the user-agent that iPad uses.

Instead of (for example)

Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10

it become (for example)

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15

My question is how can we distinguish between iPad and mac now?

like image 869
zvi Avatar asked Jul 08 '19 12:07

zvi


People also ask

What is the difference between iPadOS and macOS?

The major difference between the macOS and iOS is the interface and the framework. The macOS is a computer-based and mouse-keyboard operating system, whereas the iOS is a touch screen interfacing operating system. The application framework in iOS is the UIKit, and the macOS uses the AppKit.

Whats the difference between iOS and iPadOS?

iPadOS is a mobile operating system developed by Apple Inc. for its iPad line of tablet computers. It is a rebranded variant of iOS, the operating system used by Apple's iPhones, renamed to reflect the diverging features of the two product lines, particularly the iPad's multitasking capabilities.

How does iPad and Mac work together?

Handoff makes it possible. When your Mac, iOS, or iPadOS devices are near each other, they can automatically pass what you're doing from one device to another. An icon representing the last app you were using will appear on your second device — in the Dock on your Mac or iPad or in the App Switcher on your iPhone.

Is iPadOS like macOS?

I can see iPadOS developing in a direction that, for the most part, makes the experience for most users more or less indistinguishable from macOS — and in turn, makes using the two side by side more seamless than ever. But this absolutely doesn't mean iPadOS will behave like macOS under the hood.


3 Answers

The condition I used to detect IpadOS:

ua.toLowerCase().indexOf('macintosh') > -1 && navigator.maxTouchPoints && navigator.maxTouchPoints > 2
like image 97
quangh Avatar answered Oct 04 '22 14:10

quangh


Unfortunately doing so via the User-Agent string alone doesn't seem to be possible anymore. This is a bit of an issue if you're server-side and need to share downloads differently as you mentioned in one of your comments.

However, the below should reliably detect iPads on the client-side:

const iPad = !!(navigator.userAgent.match(/(iPad)/)
  || (navigator.platform === "MacIntel" && typeof navigator.standalone !== "undefined"))

It avoids relying on touch events which will occur on Macs with touch screen monitors, and instead uses navigator.standalone which is an iOS only property.

like image 40
GuyC Avatar answered Oct 04 '22 14:10

GuyC


Combining quangh's answer and Michael Zaporozhets's answer to detect mobile devices including iPads.

detectMobile() {
  let isMobile = RegExp(/Android|webOS|iPhone|iPod|iPad/i)
   .test(navigator.userAgent);

  if (!isMobile) {
    const isMac = RegExp(/Macintosh/i).test(navigator.userAgent);

    if (isMac && navigator.maxTouchPoints && navigator.maxTouchPoints > 2) {
      isMobile = true;
    }
  }
  return isMobile;
}
like image 26
BJax Avatar answered Oct 04 '22 15:10

BJax