20

我有一个为 ios 和 android 设备部署的流星应用程序,我希望某些代码仅在 ios 设备上运行,而不是在 android 上运行。我知道我可以使用 navigator.userAgent 检测设备,但这只有在我的应用程序在浏览器上运行时才有效。

//如果它是浏览器,这有效

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

但是,如果我使用流星科尔多瓦插件为 android 创建了捆绑包,是否有任何可能的方法来检测设备,这样它就可以像本机应用程序一样工作。

4

2 回答 2

32

没有插件的原因,只需使用窗口。

window.cordova.platformId

您还可以获得操作系统的版本。

完整输出:

window.cordova.platformId

“安卓”

window.cordova.platformVersion

“7.0.0”

于 2018-05-09T12:17:39.427 回答
16

正如 Nijil Nair 建议的那样,使用 Cordova Device Plugin。如果您在添加插件时需要帮助,请参阅Meteor Cordova。正确安装插件后,您可以使用var devicePlatform = device.platform;它将根据设备返回以下内容之一:

  • “安卓”
  • “黑莓”
  • “iOS”
  • “WinCE”
于 2015-08-19T14:12:14.373 回答