iOS原生OC向React Native傳送訊息、事件、通知
RCTEventEmitter
此篇僅獻給剛剛入門的同志們。
大家在使用React Native的時候,都會比較關心原生和React Native的互動問題。React Native給原生髮送訊息,在中文官網上講得也比較明白,按照上面的例子,相信大家都可以實現出來。但是在原生給React Native傳送訊息的時候,你會發現試了很多次,可能就是不成功,想想也是不知道為啥,對於剛剛上路的小夥伴來說,這真的是讓人非常煩惱,不要著急,如果你看到了這篇文章,恭喜你,可以好好去喝一杯咖啡了。官方文件介紹,iOS端繼承RCTEventEmitter類,使用sendEventWithName方法。
- (void)sendEventWithName:(NSString *)name body:(id)body;
在網上搜了很多資料,發現介紹的都是0.28以前的方法RCTDeviceEventEmitter,但是現在這個方法廢棄了,不建議使用了。
/** * Deprecated, do not use. */ - (void)sendDeviceEventWithName:(NSString *)name body:(id)body __deprecated_msg("Subclass RCTEventEmitter instead");
不知道大家在使用RCTEventEmitter類中這個方法sendEventWithName的時候,有沒有碰到如下問題:
*** Assertion failure in -[RCTEventEmitter sendEventWithName:body:]() Bridge is not set. This is probably because you've explicitly synthesized the bridge in WSNotification, even though it's inherited from RCTEventEmitter
如下程式碼所示,如果是通過[WSNotification alloc] init]; 來初始化,然後呼叫OCsendMessageToReactNative 方法,就會報_bridge為空的異常。通過新增自定義支援的方法名(在-(NSArray<NSString *> *)supportedEvents {} 中設定)。該方法在React Native中存在對應的監聽事件(.addListener('OCSendToRN',(reminder) =>);)時就會呼叫。通過supportedEvents {} 方法中列印,我們知道,該WSNotification類在這個回撥中已經初始化好了,而我們在需要的地方還通過[WSNotification alloc] init]; 這樣的方式初始化,就會是另一個新的物件了,和原有的WSNotification (RCTEventEmitter)已經不是一個東東了。
由此可知,RCTEventEmitter物件,不需要我們來初始化。直接呼叫即可。
// WSNotification.h #import <React/RCTEventEmitter.h> #import <React/RCTBridgeModule.h> @interface WSNotification : RCTEventEmitter <RCTBridgeModule> -(void)OCsendMessageToReactNative:(NSDictionary *)dictionary; @end ----------------------------------------- // WSNotification.m #import "WSNotification.h" @interface WSNotification() @end @implementation WSNotification RCT_EXPORT_MODULE(); - (NSDictionary<NSString *, NSString *> *)constantsToExport { return @{@"name": @"OCSendToRN"}; } -(NSArray<NSString *> *)supportedEvents { NSLog(@"----------->supportedEvents--->%@ self-->%@",self.bridge, self); return@[@"OCSendToRN"]; } -(void)startObserving { NSLog(@"startObserving------>%@ self--->%@",self.bridge,self); } -(void)stopObserving { } -(void)OCsendMessageToReactNative:(NSDictionary *)dictionary { NSLog(@"OCsendMessageToReactNative------>%@ self--->%@",self.bridge, self); [self sendEventWithName:@"OCSendToRN" body:dictionary]; NSLog(@"oc傳送RN----->OCSendToRN"); } @end
敲黑板-----------------------
如何避免不重新建立WSNotification,而不影響使用[self sendEventWithName:@"OCSendToRN" body:dictionary];
使用通知
在-(void)startObserving;開啟通知在-(void)stopObserving;移除通知
// WSNotification.h #import <React/RCTEventEmitter.h> #import <React/RCTBridgeModule.h> @interface WSNotification : RCTEventEmitter <RCTBridgeModule> +(void)OCsendMessageToReactNative:(NSDictionary *)dictionary; @end ------------------------------- #import "WSNotification.h" @interface WSNotification() @end @implementation WSNotification RCT_EXPORT_MODULE(); - (NSDictionary<NSString *, NSString *> *)constantsToExport { return @{@"name": @"OCSendToRN",@"ocName":@"OC直接定義的常量"}; } -(NSArray<NSString *> *)supportedEvents { NSLog(@"----------->supportedEvents--->%@ self-->%@",self.bridge, self); return@[@"OCSendToRN"]; } -(void)startObserving { NSLog(@"startObserving------>%@ self--->%@",self.bridge,self); for (NSString *notifiName in [self supportedEvents]) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(OCsendToReactNative:) name:notifiName object:nil]; } } -(void)stopObserving { // 在此移除通知 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"OCSendToRN" object:nil]; NSLog(@"oc----->在此移除通知"); } +(void)OCsendMessageToReactNative:(NSDictionary *)dictionary { [[NSNotificationCenter defaultCenter] postNotificationName:@"OCSendToRN" object:nil userInfo:dictionary]; } -(void)OCsendToReactNative:(NSNotification *)notification { NSLog(@"OCsendToReactNative------>%@ self--->%@",self.bridge, self); [self sendEventWithName:@"OCSendToRN" body:notification.userInfo]; NSLog(@"oc傳送RN----->%@",notification.userInfo[@"name"]); } @end
對應的React Native
//引入檔案 import {NativeModules, NativeEventEmitter} from 'react-native'; const { WSNotification } = NativeModules; console.log('接收OC定義的常量--->'+WSNotification.name+' -------->'+WSNotification.ocName); const calendarManagerEmitter = new NativeEventEmitter(WSNotification); const subscription = calendarManagerEmitter.addListener('OCSendToRN',(reminder) => console.log('RN收到OC發來---->'+reminder.name), // 通過OC中constantsToExport 傳過來的引數,動態設定監聽的方法 // const subscription = calendarManagerEmitter.addListener(WSNotification.name,(reminder) => console.log('RN收到OC發來---->'+reminder.name); // 別忘了取消訂閱,通常在componentWillUnmount生命週期方法中實現。 // subscription.remove();
React Native還提供了,由原生直接設定變數給React Native,在初始化的時候,就已經設定好了,不支援後期動態更改。使用如下方法,即可直接轉參到React Native。
- (NSDictionary<NSString *, NSString *> *)constantsToExport;