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

后端 未结 10 859
花落未央
花落未央 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:04

    I use this solution: https://stackoverflow.com/a/48709199/2400373

    My solution final is this:

    import { Platform } from "react-native";
    
    Dispositivo() {
        if (Platform.isPad == true) {
          return(
           <Text>test</Text>
          )
        }
    

    Then I call this function:

    {this.Dispositivo()}
    
    0 讨论(0)
  • 2021-02-01 17:06

    You can roughly determine what iOS device is being used without any external dependencies... First query Platform.OS then the Dimensions module allows you to query the device for screen dimensions which can be translated to devices: http://iosres.com/

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

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

    $ react-native link


    if(Device.isIos) {
    
    }
    
    if(Device.isTablet) {
    
    }
    
    0 讨论(0)
  • 2021-02-01 17:10

    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.

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

    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!)
    0 讨论(0)
  • 2021-02-01 17:17

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

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

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