新建继承NSObject的数据类NativeCommon
。
.h文件
- 修改继承为
RCTEventEmitter
。 - 导入头文件
#import <React/RCTBridgeModule.h>
#import <React/RCTLog.h>
#import <React/RCTEventEmitter.h>
.m文件
- 必须写
RCT_EXPORT_MODULE();
- 不带参数
RCT_EXPORT_METHOD(goAliVerifyPage) {
}
- 接收字符串
RCT_EXPORT_METHOD(addHelloWord:(NSString *)name location:(NSString *)location){
RCTLogInfo(@"带参数---->name=%@,location=%@",name,location);
}
-
RCTResponseSenderBlock
只接受一个参数——传递给 JavaScript 回调函数的参数数组。
RCT_EXPORT_METHOD(checkIsRoot:(RCTResponseSenderBlock)callback {
NSArray *array = @[@"string", @"number"];
callback(array);
}
-
resolve()
回调给RN的参数,回调的错误信息
RCT_EXPORT_METHOD(getHBDeviceUniqueID:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
NSArray *events =@[@"Promise ",@"test ",@" array"];
if (events) {
resolve(events);
} else {
NSError *error=[NSError errorWithDomain:@"我是回调错误信息..." code:101 userInfo:nil];
reject(@"no_events", @"There were no events", error);
}
}
网友评论