【问题标题】:Meteor device detection android or ios?流星设备检测android还是ios?
【发布时间】:2015-11-11 15:21:22
【问题描述】:

我有一个为 ios 和 android 设备部署的流星应用程序,我希望某些代码仅在 ios 设备上运行,而不是在 android 上运行。我知道我可以使用流星设备检测包检测设备,例如

Meteor.Device.isPhone()

但是有什么方法可以知道它是安卓设备还是iOS设备。

编辑:我使用流星科尔多瓦创建了捆绑包。

【问题讨论】:

    标签: meteor device-detection


    【解决方案1】:

    这里有一个全局助手,应该可以检测 iOS:

    Template.registerHelper('isIOS',() => {
      return ( navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false );
    });
    

    另一个适用于 Android:

    Template.registerHelper('isAndroid',() => {
      return navigator.userAgent.toLowerCase().indexOf("android") > -1;
    });
    

    在客户端 js 中的任何地方使用:

    Blaze._globalHelpers.isIOS()
    Blaze._globalHelpers.isAndroid()
    

    当然,在 html 模板标记中使用:

    {{#if isIOS}}...{{/if}}
    {{#if isAndroid}}...{{/if}}
    

    【讨论】:

    • 你在 android 上使用过 indexof,在 ios 上使用过 match,这有什么不同
    • 并非如此 - Android 更简单,因为它只有一种风格。 iOS 3.
    • 这仅适用于浏览器,但如果我使用流星科尔多瓦创建捆绑包会怎样
    • 对不起,necro,但在内部如何反应?
    • This SO answer 应该可以帮助您使用 React。只需定义函数并导出它们,然后在你的 jsx 中使用它们。
    猜你喜欢
    • 2019-12-31
    • 2019-01-25
    • 2017-05-09
    • 2013-06-02
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多