Device detection if is android?
Asked Answered
T

2

21

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.

Trattoria answered 18/8, 2015 at 20:23 Comment(2)
Did you try Device API? You can check if(device.platform == 'Android')Mango
possible duplicate of Meteor device detection android or ios?Bellflower
T
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"
Twill answered 19/8, 2015 at 14:12 Comment(3)
Meteor Cordova link has moved to: guide.meteor.com/mobile.html#introductionDanseuse
Not all versions of Windows return WinCE, check this out for more.Pulsar
From the recent code documentation: ` /** * The device platform (lowercase). * * @since 1.0.0 */ platform: 'ios' | 'android' | 'web';`Hoarfrost
C
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"

Calmas answered 9/5, 2018 at 12:17 Comment(3)
Where is the documentation of this field? How to check what values are possible?Epilate
Is this safe to use? Can we rely on this?Epilate
@Epilate 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.jsVisby
T
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"
Twill answered 19/8, 2015 at 14:12 Comment(3)
Meteor Cordova link has moved to: guide.meteor.com/mobile.html#introductionDanseuse
Not all versions of Windows return WinCE, check this out for more.Pulsar
From the recent code documentation: ` /** * The device platform (lowercase). * * @since 1.0.0 */ platform: 'ios' | 'android' | 'web';`Hoarfrost

© 2022 - 2024 — McMap. All rights reserved.