1、创建原生模块,实现“RCTBridgeModule”协议
#import <Foundation/Foundation.h>
#import "RCTBridgeModule.h"
@interface NativeTest : NSObject <RCTBridgeModule>
@end
2、导出模块,导出方法
#import "NativeTest.h"
@implementation NativeTest
//导出模块
RCT_EXPORT_MODULE(); //此处不添加参数即默认为这个OC类的名字
//导出方法,桥接到js的方法返回值类型必须是void
RCT_EXPORT_METHOD(doSomething:(NSString *)name)
{
NSLog(@"doSomething:%@",name);
}
@end
//创建原生模块实例
var NativeModule = require('react-native').NativeModules.NativeTest;
//方法调用
NativeModule.doSomething('ReactNative');
控制台输出:
2016-02-24 16:52:07.397 [info][tid:com.facebook.React.JavaScript][RCTJSCProfiler.m:63] JSC profiler is not supported.
2016-02-24 16:52:07.974 RN_StockChart[39722:2233173] doSomething:ReactNative