|
| 1 | +# How to Detect iPad using JavaScript |
| 2 | + |
| 3 | +iPads used to have a very clear user-agent string that included the keyword `iPad`, for example: |
| 4 | + |
| 5 | +`Mozilla/5.0 (iPad; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) FxiOS/1.0 Mobile/12F69 Safari/600.1.4` |
| 6 | + |
| 7 | +However, starting from iPadOS 13, Apple forces Safari to use desktop mode by default. This makes Safari on iPad started identifying itself as a desktop device (`Macintosh`) instead of `iPad`, like: |
| 8 | + |
| 9 | +`Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15` |
| 10 | + |
| 11 | +This makes user-agent detection not reliable when detecting iPad. As a workaround, you can use [`withFeatureCheck()`](/api/main/idata/with-feature-check) method to also detect for some iPad-specific browser features in addition to the usual user-agent detection. |
| 12 | + |
| 13 | +## Code Example |
| 14 | + |
| 15 | +### User-Agent Detection Only |
| 16 | + |
| 17 | +```js [user-agent-detect.js] |
| 18 | +import { UAParser } from 'ua-parser-js'; |
| 19 | + |
| 20 | +const parser = new UAParser(); |
| 21 | +const device = parser.getDevice(); |
| 22 | + |
| 23 | +if (device.is('iPad')) { |
| 24 | + console.log("Hi, looks like you're using an iPad!"); |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +### User-Agent Detection + Feature Detection |
| 29 | + |
| 30 | +```js [with-feature-detect.js] |
| 31 | +import { UAParser } from 'ua-parser-js'; |
| 32 | + |
| 33 | +const parser = new UAParser(); |
| 34 | +const device = await parser.getDevice().withFeatureCheck(); |
| 35 | + |
| 36 | +if (device.is('iPad')) { |
| 37 | + console.log("Hi, looks like you're using an iPad"); |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +## References |
| 42 | + |
| 43 | +* [iPadOS brings breaking changes for developers🡥](https://docs.getupdraft.com/ios/ipados) *—Updraft Blog* |
| 44 | +* [`withFeatureCheck()`](/api/main/idata/with-feature-check) |
0 commit comments