反应本机发送事件

时间:2019-04-30 10:32:44

标签: objective-c react-native

我在将事件从Objective C模块发送到React Native时遇到问题。我已经完成了PlayerEvents类,并从其他这样的类中调用它:

 self.events = [PlayerEvents allocWithZone:nil];
[self.events sendEventWithName:[self formatTypeToString:onBuffering] body:@{@"isBuffering": [NSNumber numberWithBool:YES]}];


#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>

@interface PlayerEvents : RCTEventEmitter <RCTBridgeModule>

@end

@implementation PlayerEvents {
bool hasListeners;
}

RCT_EXPORT_MODULE();

+(id)allocWithZone:(NSZone *)zone {

static PlayerEvents *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    sharedInstance = [super allocWithZone:zone];
});
return sharedInstance;
}

+ (BOOL)requiresMainQueueSetup {
    return NO;
}

- (NSArray<NSString *> *)supportedEvents {
    return @[@"onMediaLoaded", @"onPlaying", @"onPause", @"onError", @"onEnded", @"onBuffering", @"onTimeUpdate", @"onStateUpdate"];
 }

 // Will be called when this module's first listener is added.
- (void)startObserving {
     hasListeners = YES;
  }

  // Will be called when this module's last listener is removed, or on dealloc.
- (void)stopObserving {
    hasListeners = NO;
}

- (void)sendEventName:(NSString *)eventName body:(id)body {
    if (hasListeners) {
        [self sendEventWithName:eventName body:body];
}
}
@end

但是我得到这个错误:

  

发送事件时出错:正文:{isBuffering = 1} onBuffering未设置网桥。
这可能是因为您已经在PlayerEvents中明确合成了桥,即使它是从RCTEventEmitter继承的。

我该怎么办?

感谢克劳迪(Claudiu)。

0 个答案:

没有答案