React Native:如何确定设备是iPhone还是iPad

时间:2016-10-25 20:59:52

标签: react-native

我知道React Native我们能够确定是使用counts %>% ggplot(aes(x = x, y = y , fill = diff)) + geom_hex(stat = "identity") + coord_equal() + scale_fill_gradient2() 模块运行iOS还是Android,但我们如何确定在iOS上使用的设备?

10 个答案:

答案 0 :(得分:22)

自9.02.2018起,还有

import { Platform } from 'react-native'
Platform.isPad // boolean

请注意(截至目前)它没有安卓对手。

答案 1 :(得分:16)

最简单的方法是使用宽高比。代码将是:

import { Dimensions } from 'react-native';
const {height, width} = Dimensions.get('window'); 
const aspectRatio = height/width;

if(aspectRatio>1.6) {

   // Code for Iphone

}
else {

   // Code for Ipad

}

iPad的宽高比为4:3(1.334),iPhone的宽高比为16:9(1.778)

在应用上述逻辑之前,请务必使用Platform.OS === 'ios'方法检查您是否在iOS设备上。

答案 2 :(得分:9)

如果您正在寻找一种方法,而不包括第三方库(如react-native-device-info),您也可以这样做:

import { NativeModules } from 'react-native';
const { PlatformConstants } = NativeModules;
const deviceType = PlatformConstants.interfaceIdiom;

deviceType可以使用以下值:phonepadtvcarplayunknown

答案 3 :(得分:7)

您可以粗略地确定正在使用的iOS设备而没有任何外部依赖性...首先查询Platform.OS然后Dimensions模块允许您查询设备以查找可以转换为设备的屏幕尺寸:http://iosres.com/

答案 4 :(得分:5)

您应该可以从模块react-native-device-info

获取该信息

https://github.com/rebeccahughes/react-native-device-info

答案 5 :(得分:2)

react-native中有一种称为PlatformIOSStatic的类型,如果使用打字稿,则需要强制将Platform强制转换为PlatformIOSStatic

import { Platform, PlatformIOSStatic } from 'react-native'

if (Platform.OS === 'ios') {
  const platformIOS = Platform as PlatformIOSStatic
  console.log(platformIOS.isPad)
  console.log(platformIOS.isTVOS)
}

这里的界面设计非常糟糕,希望RN团队可以对其进行改进。

答案 6 :(得分:1)

我使用isTablet()通过iPhone检测ipad https://github.com/rebeccahughes/react-native-device-info

import { isTablet } from 'react-native-device-info';
if (isTablet()) {
// try something
}

答案 7 :(得分:0)

@Maxwelll的一个很好的解决方案。 更准确的方法是测量屏幕比率。 所有iPhone都是16:9,所有iPad都是3:4。

答案 8 :(得分:0)

$ npm install react-native-device-detection --save

$ react-native link


if(Device.isIos) {

}

if(Device.isTablet) {

}

答案 9 :(得分:-1)

我使用以下解决方案:https://stackoverflow.com/a/48709199/2400373

我的最终解决方案是:

https://login.windows.net/common/oauth2/authorize?response_type=code&resource=https%3A%2F%2Fmanage.office.com&client_id={xxxxxxxxxxx}&redirect_uri={xxxxxxxxxxxxx} 

然后我调用此函数:

import { Platform } from "react-native";

Dispositivo() {
    if (Platform.isPad == true) {
      return(
       <Text>test</Text>
      )
    }