20

I have an meteor app that is deployed for both ios and android device and i want certain code to run on only ios device and not on android. I know that I can detect device using navigator.userAgent but this will only work if i have my app running on browser.

//This works if its a browser

navigator.userAgent.toLowerCase().indexOf("android") > -1;

But is there any possible way to detect device if i have created bundle for android using meteor cordova plugin so it works like a native app.

Nakib
  • 4,593
  • 7
  • 23
  • 45

2 Answers2

35

No reason for plugins, just use window.

window.cordova.platformId

You can also get the version of the os.

Full output:

window.cordova.platformId

"android"

window.cordova.platformVersion

"7.0.0"

Community
  • 1
  • 1
Roee
  • 1,155
  • 3
  • 15
  • 24
  • Where is the documentation of this field? How to check what values are possible? – PlsWork Jan 20 '22 at 15:01
  • Is this safe to use? Can we rely on this? – PlsWork Jan 20 '22 at 15:17
  • @PlsWork It is safe to use. A freshly created Cordova project has this line: `console.log('Running cordova-' + cordova.platformId + '@' + cordova.version);` Check the `cordova-app-hello-world` package. Also search your Cordova project's "platforms" folder for "platformId: platform.id" and you'll see it in the various platforms, e.g. android/platform_www/cordova.js or ios/www/cordova.js – Adam Taylor Feb 18 '22 at 22:00
16

As Nijil Nair suggested use the Cordova Device Plugin . If you need help adding the plugin see Meteor Cordova . Once the plugin is properly installed you can use var devicePlatform = device.platform; which will return one of the following based on the device:

  • "Android"
  • "BlackBerry"
  • "iOS"
  • "WinCE"
Nate
  • 1,875
  • 15
  • 27
  • 1
    Meteor Cordova link has moved to: https://guide.meteor.com/mobile.html#introduction – ninjaPixel Jul 24 '17 at 18:38
  • Not all versions of Windows return `WinCE`, check [this](https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-device/index.html#deviceplatform) out for more. – doubleOrt Jul 29 '17 at 01:44
  • From the recent code documentation: ` /** * The device platform (lowercase). * * @since 1.0.0 */ platform: 'ios' | 'android' | 'web';` – Mundi Jul 06 '22 at 17:01