2

I tried to use the Ionic Platform module, but it fails to detect that an Ipad is in action.

constructor(
    private platform: Platform,
  ) {
  }
...
console.log(platform.platforms());

logs iphone,ios,cordova,mobile,hybrid so no help here.

Tried to check the user agent, it says iPhone, not iPad now. Also tried to use this plugin, returns false as well in at least the four different emulators I tested (Ipad pro, Ipad air 2...).

Is there a way to make this work?

Jeremy Belolo
  • 4,319
  • 6
  • 44
  • 88
  • Possible duplicate of [How to get the Iphone type from simulator (IOS)](https://stackoverflow.com/questions/33493800/how-to-get-the-iphone-type-from-simulator-ios) – ceejayoz Jul 19 '19 at 16:11
  • 2
    @ceejayoz the question is about the detection with the Ionic Framework, not Swift – matthiasunt Jul 19 '19 at 19:18

3 Answers3

2

Use device.model from cordova-plugin-device

jcesarmobile
  • 51,328
  • 11
  • 132
  • 176
1

You will need to use the device.model from cordova-plugin-device.

Now, we know this will return you the model, but to further narrow down to individual models, you need to get an Identifier for each model from the list here. Once you have an Identifier you can apply basic conditions like below to get individual models.

Example 1: For iPhone X

if (device.model.includes('iPhone10,3') || device.model.includes('iPhone10,6')) {
  // custom logic for iPhone X
}

Example 2: For iPad mini (5th generation)

if (device.model.includes('iPad11,1') || device.model.includes('iPad11,2')) {
   // custom logic for iPad mini (5th generation)
}

NOTE: This might not work correctly if testing on a simulator.

Nidhin Joseph
  • 9,981
  • 4
  • 26
  • 48
  • Hum... Thanks, as I said on the other similar answer, yes it doesn't work correctly on a simulator. Vote for clarity of the answer though. – Jeremy Belolo Jul 20 '19 at 18:55
  • As mentioned, this solution does not work in simulators as all simulators returns `x86_64`. Just curious to know why you would need this to work on a simulator than on an actual device? – Nidhin Joseph Jul 20 '19 at 22:04
  • Oh, that's simple... I'm using a MacInCloud to build for Ios, using testFlight as needed to check how it behaves on an actual device but resorting to the local emulators for most of the work – Jeremy Belolo Jul 21 '19 at 11:52
  • @JeremyBelolo But, I assume that your final app runs on an actual device and the above solution will work. – Nidhin Joseph Jul 22 '19 at 03:03
  • Yes, the final app... Except that it was to workaround a bug that prevents me from submitting my app because Apple rejects it since it doesn't work on their emulators haha. Will try and find another solution. – Jeremy Belolo Jul 22 '19 at 07:12
0

You could try the following in your constructor:

platform.ready().then(() => {
    if (this.platform.is('ipad')) {
      console.log("Hey iPad!");
    }
});

Also, see the docs for further reference.

matthiasunt
  • 709
  • 1
  • 12
  • 32