【问题标题】:Detecting the device OS in react-native (without using the platform api)在 react-native 中检测设备操作系统(不使用平台 api)
【发布时间】:2021-09-15 14:16:06
【问题描述】:

我正在尝试检测应用程序是在 android 还是 androidTV 上运行,但由于我问过 here 的问题,我无法使用 react-native's platform api

还有其他软件包可以让我这样做吗?或者我可以自己用javascript写吗?

【问题讨论】:

    标签: android react-native android-tv


    【解决方案1】:

    您可以使用来自react-native-device-info https://github.com/react-native-device-info/react-native-device-info#getDeviceTypegetDeviceType 助手

    import React from 'react';
    import { View, Text } from 'react-native';
    import { getDeviceType } from 'react-native-device-info';
    
    const deviceType = getDeviceType();
    
    const App = () => {
      return (
        <View>
          <Text>{deviceType}</Text>
        </View>
      );
    };
    
    export default App;
    

    【讨论】:

    • import typeof AccessibilityInfo from './Libraries/Components/AccessibilityInfo/AccessibilityInfo'; ^^^^^^ SyntaxError: Cannot use import statement outside a module 我在尝试像这样导入时遇到此错误const info = require('react-native-device-info');
    • 您使用的是 Expo 还是 RN CLI?
    • 我正在使用 RN CLI
    • 你用的是什么版本的react-native包?
    • react-native-tvos@^0.63.4-0
    【解决方案2】:

    我不知道这是否会解决您的特定用例,但您可以通过在文件名中指定每个平台的特定文件:

    index.android.js
    index.ios.js
    

    React Native 会根据平台选择使用哪个文件。

    【讨论】:

    【解决方案3】:

    从 React Native 尝试平台模块

    import { Platform } from "react-native"
    
    isTV === Platform.constants.uiMode === "tv"
    

    https://reactnative.dev/docs/platform#properties

    【讨论】:

      猜你喜欢
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      • 2014-09-17
      • 1970-01-01
      • 2013-11-19
      • 2014-06-21
      • 2013-09-07
      相关资源
      最近更新 更多