在原生与js通讯时,用RCTEventEmitter,会遇到以下的坑
bridge is not set. This is probably because you’ve explicitly synthesized the bridge
其原因由于RCTEventEmitter为nsobject对象,但却不能像一般的NSObject对象一样进行初始化,调用实例方法 ,以上错误出现在实例化了一个RCTEventEmitter类型的对象,并调用了他的实例方法,实际使用应在addObserving等方法中进行实例方法的调用,而通过注册通知来实现方法的调用。
解决方法如下
@interface RNManager : RCTEventEmitter
+ (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload;
@end
@implementation RNManager
RCT_EXPORT_MODULE()
- (void)startObserving {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(emitEventInternal:) name:@"event-emitted" object:nil];
}
- (void)stopObserving {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)emitEventInternal:(NSNotification *)notification{
[self sendEventWithName:@"changeToken" body:notification.userInfo];
}
+ (void)emitEventWithName:(NSString *)name andPayload:(NSDictionary *)payload{ [[NSNotificationCenter defaultCenter] postNotificationName:@"event-emitted" object:self userInfo:payload];
}
- (NSArray*)supportedEvents {
return @[@"changeToken"];
}
- (void)changeToken:(NSString *)token {
[self sendEventWithName:@"changeToken" body:@{@"token" : token}];
}
@end
直接调用类方法,在类方法中发内部通知给对象就好了,这个通讯的对象在js代码中会自动创建,不需要自身去创建的
JS代码的实现如下
componentDidMount() {
constmyModuleEvt=newNativeEventEmitter(NativeModules.RNManager)
myModuleEvt.addListener('changeToken', (msg)=>{
console.log('i have recieve some ',msg)
})
}
这样子就可以接受原生发过来的消息了