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

后端 未结 10 841
花落未央
花落未央 2021-02-01 16:32

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 i

相关标签:
10条回答
  • 2021-02-01 17:22

    if using TypeScript, there is a type in react-native called PlatformIOSStatic, you need to force-cast Platform to PlatformIOSStatic.

    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 here is pretty bad, hope RN team would improve it.

    0 讨论(0)
  • 2021-02-01 17:25

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

    0 讨论(0)
  • 2021-02-01 17:26

    I used isTablet() to detect ipad with iphone https://github.com/rebeccahughes/react-native-device-info

    import { isTablet } from 'react-native-device-info';
    if (isTablet()) {
    // try something
    }
    
    0 讨论(0)
  • 2021-02-01 17:29

    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.

    0 讨论(0)
提交回复
热议问题