2

I'm trying to build a react-native wrapper around our existing native android and ios SDK. I want to know if I can use the same class name and class methods for both the Android and iOS bridge modules? And how do I map the right module to be called for the right device?

For instance, for iOS :

// CalendarManager.h
#import "RCTBridgeModule.h"
@interface CalendarManager : NSObject <RCTBridgeModule>
@end

// CalendarManager.m
@implementation CalendarManager
RCT_EXPORT_MODULE();
@end

RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location)
{
    //Some work
}

For Android :

public class CalendarManager extends ReactContextBaseJavaModule {

  public CalendarManager(ReactApplicationContext reactContext) {
    super(reactContext);
  }

  @Override
  public String getName() {
    return "CalendarManager";
  }

  @ReactMethod
  public void addEvent(String name, String location) {
    //Some work
  }
}

And in my js file, if I have something like this :

import { NativeModules } from 'react-native';
NativeModules.CalendarManager.addEvent("Name", "Location")

Which one will be executed? Is there a way where I could route it to the right function based on the device?

Thanks.

4

2 回答 2

2

你不必自己做那个路由。这是内置在 React-Native 中的。它知道您使用的是哪种类型的设备。

于 2017-01-06T16:45:04.717 回答
1

根据您在创建应用程序 APK 或 IPA 时设置的目标平台,React Native 会打包您在项目中拥有的不同版本的代码。也就是说,react native run-android例如,当您这样做时,它将编译位于android文件夹中的 Android 项目中的本机 Java 代码,并将其打包到 APK 中。然后 JavaScript 将可以访问本机 Java 代码。使用 Objective-C/Swift 代码的 iOS 也是如此。所以是的,您可以(实际上,我会说您必须)对本机模块使用相同的名称。

于 2017-01-06T19:51:12.813 回答