Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native: How to Determine if Device is iPhone or iPad

Tags:

react-native

I know with React Native that we have the ability to determine whether iOS or Android is being run using the Platform module, but how can we determine what device is being used on iOS?

like image 846
Steed-Asprey Avatar asked Oct 25 '16 20:10

Steed-Asprey


People also ask

How do I know which React Native iPhone I have?

DeviceInfo. getModel() - to get the device model, for example, iPhone X. DeviceInfo. getSystemVersion() - to get the system version, for example, 12.0 or 9.0.

How do I know if my tablet is in React Native?

You can use the react-native-device-info package along with the Dimensions API. Check the isTablet() method and apply different styles according on the result.


3 Answers

As of 9.02.2018 there is also

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

Mind that (as of now) it has no android counterpart.

  • IOS https://github.com/facebook/react-native/blob/master/Libraries/Utilities/Platform.ios.js#L23
  • Android https://github.com/facebook/react-native/blob/master/Libraries/Utilities/Platform.android.js (no isPad!)
like image 110
Artur Kulig Avatar answered Sep 28 '22 05:09

Artur Kulig


Simplest approach will be using the aspect ratio. The code will be:

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

}

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

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

like image 21
Dani Akash Avatar answered Sep 28 '22 03:09

Dani Akash


If you're looking for a way to do that without including 3rd party libraries (like react-native-device-info) you can also do:

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

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

like image 11
SudoPlz Avatar answered Sep 28 '22 04:09

SudoPlz