React Native: No visible @interface for 'MyModule' declares the selector 'sendEventWithName:body:'
Question
In the iOS project, I use Objective C to customize a native module, and use sendEventWithName
to send an event to the Javascript module.
// MyModule.h
#import <React/RCTBridgeModule.h>
@interface MyModule : NSObject <RCTBridgeModule>
@end
// MyModule.m
#import "MyModule.h"
@implementation MyModule
RCT_EXPORT_MODULE();
- (NSArray<NSString *> *)supportedEvents
{
return @[@"EventReminder"];
}
- (void)calendarEventReminderReceived:(NSNotification *)notification
{
NSString *eventName = notification.userInfo[@"name"];
[self sendEventWithName:@"EventReminder" body:@{@"name": eventName}];
}
@end
But Xcode reports an error.
No visible @interface for 'MyModule' declares the selector 'sendEventWithName:body:'
Solution
sendEventWithName
is a method of the RCTEventEmitter
class, the MyModule.h needs to inherit RCTEventEmitter
instead of NSObject
.
// MyModule.h
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
@interface MyModule : RCTEventEmitter <RCTBridgeModule>
@end