javascript 如何检测 iPhone X(离子 - 科尔多瓦应用程序)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46704773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:52:26  来源:igfitidea点击:

How to detect iPhone X (Ionic - cordova app)

javascriptcordovaionic-frameworkiphone-x

提问by Ernesto Schiavo

I need to make some changes to my app but only for iPhone X.

我需要对我的应用程序进行一些更改,但仅限于 iPhone X。

The app is Apache Cordova based (with Ionic framework).

该应用程序基于 Apache Cordova(使用 Ionic 框架)。

Is there a cordova pluginto detect the iPhone X? If the answer is no, which is the best method to know if the user has an iPhone X in javascript?

有没有cordova插件来检测iPhone X?如果答案是否定的,那么哪种方法是了解用户是否使用javascript的 iPhone X 的最佳方法?

Thanks

谢谢

采纳答案by Maxim Shoustin

Check: var deviceInformation = ionic.Platform.device();

查看: var deviceInformation = ionic.Platform.device();

From Ionic bundle.js

来自Ionic bundle.js

/**
     * @ngdoc method
     * @name ionic.Platform#device
     * @description Return the current device (given by cordova).
     * @returns {object} The device object.
     */
    device: function() {
      return window.device || {};
    },


Also check cordova-plugin-device

同时检查cordova-plugin-device

Properties

特性

device.cordova       // returns CDV_VERSION
 device.model
 device.platform     // always returns iOS
 device.uuid
 device.version
 device.manufacturer // always returns  Apple
 device.isVirtual    // not relevant
 device.serial 

This plugin calls CDVDevice.m-> UIDeviceso if you still cannot fetch iPhone Xworth to find the way how to detect it in Obj-C and change CDVDevice.m.

这个插件调用CDVDevice.m->UIDevice所以如果你仍然无法获取iPhone X价值,请找到如何在 Obj-C 中检测它并更改它的方法CDVDevice.m



Also check this QA: iOS devices return different format device model, why?

还要检查这个 QA:iOS 设备返回不同格式的设备模型,为什么?

回答by Made in Moon

For cordova

对于科尔多瓦

using cordova-plugin-deviceplugin like so:

像这样使用cordova-plugin-device插件:

window.device.model

window.device.model

will give:

会给:

iPhone10,3or iPhone10,6

iPhone10,3或者 iPhone10,6

See doc:

文档

enter image description here

在此处输入图片说明

For browser

对于浏览器

see this comment

看到这个评论

回答by IonicBurger

I did the following using the built in ionic.Platform.device() method. I don't think it's necessary to install a whole new plugin if you are already using ionic.

我使用内置的 ionic.Platform.device() 方法执行了以下操作。如果您已经在使用 ionic,我认为没有必要安装一个全新的插件。

let model = ionic.Platform.device().model
$scope.deviceIphonex = model.includes('iPhone10')

I can then use this anywhere in my app and do things specifically for the iphone X.

然后我可以在我的应用程序的任何地方使用它,并专门为 iphone X 做一些事情。

回答by Hymansonkr

I put together this es6 to check for iphone 10and above

我把这个 es6 放在一起检查iphone 10及更高版本

const isIphoneX = () => {
    try {
        const iphoneModel = window.device.model;
        const m = iphoneModel.match(/iPhone(\d+),?(\d+)?/);
        const model = +m[1];

        if (model >= 10) { // is iphone X
            return true;
        }
    } catch (e) { }

    return false;
}

** EDIT **

** 编辑 **

I believe I was using cordova-plugin-deviceas my project was not an ionic application. I rewrote the regex so it could work with ionic.Platform.device().modeltoo.

我相信我使用的cordova-plugin-device是因为我的项目不是离子应用程序。我重写了正则表达式,以便它也可以使用ionic.Platform.device().model