React Native: how to determine if an iPhone or iPad is a device

I know with React Native that we have the ability to determine whether iOS or Android starts using the Platform module, but how can we determine which device is used in iOS?

+19
source share
10 answers

As of February 9, 2018

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

Keep in mind that (at the moment) it has no analogue for Android.

+24
source

The simplest approach would be to use aspect ratio. The code will look like this:

 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 } 

The aspect ratio of the iPad is 4: 3 (1.334) and the aspect ratio of the iPhone is 16: 9 (1.778)

Make sure you are using an iOS device using the Platform.OS === 'ios' method before applying the above logic.

+16
source

If you are looking for a way to do this without including third-party libraries (e.g. response-native-device-info), you can also do the following:

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

deviceType can take the values: phone , pad , tv , carplay and unknown .

+9
source

You can roughly determine which iOS device is being used without any external dependencies ... First, request Platform.OS then the Dimensions module to request the device screen sizes that can be transferred to devices: http://iosres.com/

+7
source

You should be able to get this information from the react-native-device-info module

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

+5
source

There is a PlatformIOSStatic type in the responsive native, you need to force the Platform to PlatformIOSStatic when typing.

 import { Platform, PlatformIOSStatic } from 'react-native' if (Platform.OS === 'ios') { const platformIOS = Platform as PlatformIOSStatic console.log(platformIOS.isPad) console.log(platformIOS.isTVOS) } 

The interface design is pretty bad here, I hope the RN team will improve it.

+5
source

Very simple

  import { Platform} from 'react-native' if (Platform.OS === 'android') { //do something... } 

Or if you need more information about the device:

You can use Just use NativeModules.RNDeviceInfo

There are many variables that will surely help you:

  import {NativeModules} from 'react-native' console.log("NativeModules.RNDeviceInfo", NativeModules.RNDeviceInfo) 

Output:

enter image description here

+1
source

Good solution by @Maxwelll. A more accurate approach is to measure the ratio of the screen. All iPhones are 16: 9, all iPads are 3: 4.

0
source

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

$ react-native link


 if(Device.isIos) { } if(Device.isTablet) { } 
0
source

Personally , I don't like using an external component to apply simple functions, so I decided that the user:

Using EXPO

The expo contains properties that help verify device information β†’ https://docs.expo.io/versions/latest/sdk/constants.html#expoconstantsplatform , for iOS you can use:

 import Expo from 'expo'; import React from 'react'; import { Text, View } from 'react-native'; class HomeComponent extends React.Component { render() { console.log(Expo.Constants.manifest); console.log(Expo.Constants.platform.ios.userInterfaceIdiom); return ( <View> <Text> {Expo.Constants.platform.ios.userInterfaceIdiom} </Text> </View> ); } } 

Using the React-Native API

However, my application also runs in android, then I used the solution raised by @Alex Pavtoulov:

 import React from 'react'; import { Text, View } from 'react-native'; const { height, width } = Dimensions.get('window'); const DeviceType = (height / width) > 1.6 ? 'Tablet' : 'Phone'; class HomeComponent extends React.Component { render() { return ( <View> <Text> This deice is: {DeviceType} </Text> </View> ); } } 
-one
source

Source: https://habr.com/ru/post/1011697/


All Articles