如何在Android中正确使用React Native中仅限iOS的组件而不出错

如何在Android中正确使用React Native中仅限iOS的组件而不出错,android,react-native,react-native-android,Android,React Native,React Native Android,我是个新来的本地人,测试PushNotificationIOS。但是checkpermission调用在Android上给了我一个错误 ExceptionsManager.js:61 Cannot read property 'checkPermissions' of undefined 我猜这是因为我只需要在iOS上使用这个组件。如何添加操作系统检查以仅在iOS上拨打电话 这是我的密码: componentWillMount: function() { //-- need an

我是个新来的本地人,测试PushNotificationIOS。但是checkpermission调用在Android上给了我一个错误

ExceptionsManager.js:61 Cannot read property 'checkPermissions' of undefined
我猜这是因为我只需要在iOS上使用这个组件。如何添加操作系统检查以仅在iOS上拨打电话

这是我的密码:

  componentWillMount: function() {
    //--  need an OS check here??
    PushNotificationIOS.checkPermissions((data)=> {
      console.log("in comp will mount: checking for permission")
      console.log(data.alert)
      console.log(data.badge)

我的建议是将特定于平台的代码拆分为单独的文件

React Native将检测文件何时具有.ios。或者,安卓。 扩展并在需要时从其他平台加载相关平台文件 组成部分

然后,您可以按如下方式要求该组件:

const MyFile= require('./MyFile');
像这样使用它

componentWillMount: function() {
    //--  it would call the logic of what ever platform was detected automatically
    MyFile.CallWhatEver();
它将运行特定于平台的代码

另一种方法是

平台模块

React Native提供一个模块,用于检测 应用程序正在运行。您可以使用检测逻辑来实现 特定于平台的代码。仅当零件的一小部分时使用此选项 组件是特定于平台的

还有一个
平台。选择可以接受任何值的

const Component = Platform.select({
  ios: () => //function goes here,
  android: () => require('ComponentAndroid'),
})();
链接

谢谢!平台是我需要的。:)当我有更多特定于平台的code.Np时,我将尝试单独的文件。我不确定这是否是您唯一需要的,所以我推荐了第一个示例。请接受我的回答:)我同意AKADER下面的回答,但如果在其他地方您需要执行一次性平台检查,您应该查看平台模块的文档。
if(Platform.OS === 'ios')
const Component = Platform.select({
  ios: () => //function goes here,
  android: () => require('ComponentAndroid'),
})();