iOS原生向rn传值

1.在原生页面

#import "RCTEventEmitter.h"
#import "RCTBridgeModule.h"
#import "RCTBridge.h"

@interface iOSExport : RCTEventEmitter <RCTBridgeModule>
+(void)callPostNoti:(NSMutableDictionary *)dic;
@end
#import "iOSExport.h"

@implementation iOSExport
@synthesize bridge=_bridge;
RCT_EXPORT_MODULE(iOSExport)

- (NSArray<NSString *> *)supportedEvents
{
  return @[@"EventReminder"];
}

RCT_EXPORT_METHOD(startObserving)
{
  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleNotification:) name:@"postLocation" object:nil];
}

+(void)callPostNoti:(NSMutableDictionary *)dic
{
  [[NSNotificationCenter defaultCenter]postNotificationName:@"postLocation" object:dic];
}

-(void)handleNotification:(NSNotification *)noti
{
  [self sendEventWithName:@"EventReminder" body:noti.object];
}

@end

2.在js页面

import {
  NativeModules,
  NativeEventEmitter,
} from 'react-native';

//开始监听
    var iOSExport = NativeModules.iOSExport
    iOSExport.startObserving()
    var emitter = new NativeEventEmitter(iOSExport);
    //用获取的模块创建监听器
    this.subScription = emitter.addListener("EventReminder", (body) => {
      console.info('收到', body)
      this.subScription.remove()
    })
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容