// EventEmitter.h#import "RCTBrIDge.h"#import "RCTEventdispatcher.h"@interface EventEmitter : NSObject<RCTBrIDgeModule> - (voID)emitEvent:(Nsstring *) eventname withData:(ID) eventData;@end
和实施:
// EventEmitter.m#import "EventEmitter.h"@implementation EventEmitter RCT_EXPORT_MODulE(); @synthesize brIDge = _brIDge; - (voID)emitEvent:(Nsstring *) eventname withData:(ID) eventData { NSLog( @"emitting %@ with data %@",eventname,[eventData description] ); [[_brIDge eventdispatcher] sendDeviceEventWithname:eventname body:eventData]; [[_brIDge eventdispatcher] sendAppEventWithname:eventname body:eventData]; }@end
我正在使用sendDeviceEvent和sendAppEvent,因为我无法工作.
在Js方面,我注册在我的一个模块的全局命名空间中接收这些事件(这样我就知道事件订阅将在事件发出之前发生).我这样注册:
console.log( 'ADDING EVENT ListENERS' );NativeAppEventEmitter.addListener( 'blah',test => console.log( 'TEST1',test ) );deviceeventemitter.addListener( 'blah',test => console.log( 'TEST2',test ) );
在我的日志语句中,我得到以下内容:
2016-03-19 12:26:42.501 [trace][tID:com.facebook.React.JavaScript] ADDING EVENT ListENERS2016-03-19 12:26:43.613 [name redacted][348:38737] emitting blah with data [data redacted]
所以我可以告诉我发送了一个app事件和一个带有标签blah的设备事件,我已经注册了用deviceeventemitter和NativeAppEventEmitters来监听blah事件,但是我没有在监听器中回调.
我究竟做错了什么??谢谢阅读!
解决方法 我已经尝试调度事件,当你使用[EventEmitter alloc] init]手动创建新的EventEmitter实例时,似乎没有初始化桥接器你应该让react-native创建实例.我检查了原生组件,他们正在使用 – (voID)setBrIDge:(RCTBrIDge *)桥接方法进行初始化工作.请查看RCTlinkingManager以查看示例.它使用NSNotificationCenter来处理事件.
// registering for RCtopenURLNotification evet when the module is initialised with a brIDge- (voID)setBrIDge:(RCTBrIDge *)brIDge{ _brIDge = brIDge; [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(handleOpenURLNotification:) name:RCtopenURLNotification object:nil];}// emitting openURL event to JavaScript- (voID)handleOpenURLNotification:(NSNotification *)notification{ [_brIDge.eventdispatcher sendDeviceEventWithname:@"openURL" body:notification.userInfo];}// creating RCtopenURLNotification event to invoke handleOpenURLNotification method+ (BOol)application:(UIApplication *)application openURL:(NSURL *)URL sourceApplication:(Nsstring *)sourceApplication annotation:(ID)annotation{ NSDictionary<Nsstring *,ID> *payload = @{@"url": URL.absoluteString}; [[NSNotificationCenter defaultCenter] postNotificationname:RCtopenURLNotification object:self userInfo:payload]; return YES;}总结
以上是内存溢出为你收集整理的听取本机ios中的事件全部内容,希望文章能够帮你解决听取本机ios中的事件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)