美文网首页
iOS原生向rn传值

iOS原生向rn传值

作者: 地平线上硝烟弥漫 | 来源:发表于2017-05-02 15:21 被阅读1586次

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()
    })

相关文章

网友评论

      本文标题:iOS原生向rn传值

      本文链接:https://www.haomeiwen.com/subject/evwctxtx.html