[英]How do you properly emit and receive an event from a bridged iOS module to JavaScript using React Native?
我有一個React Native應用程序,並希望從Obj-C發送一個事件來測試應用程序事件/發射系統的工作方式。 我已經閱讀了所有文檔,翻閱了本地回購的示例以了解示例,但仍然不知道我的代碼有什么問題。
我知道我的Obj-C會觸發,因為我已經逐步完成了Xcode中的代碼,並且self.bridge.eventDispatcher
不是nil
。 我還知道,通過控制台日志,模擬器中呈現的主要React Native組件會安裝並呈現。 問題是我沒有看到任何這些通知的控制台輸出。 我可能錯誤地要求這個ApplicationManager
類,或者訂閱錯誤的“東西”來監聽事件。 我可能做錯很多/很差的事情:-)
這是我的Obj-C導出模塊:
@implementation ApplicationManager
RCT_EXPORT_MODULE();
@synthesize bridge = _bridge;
- (instancetype)init {
self = [super init];
if ( self ) {
NSNotificationCenter * notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
[notificationCenter addObserver:self selector:@selector(applicationWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
[notificationCenter addObserver:self selector:@selector(applicationDidFinishLaunching:) name:UIApplicationDidFinishLaunchingNotification object:nil];
[notificationCenter addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
[notificationCenter addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
[notificationCenter addObserver:self selector:@selector(applicationDidReceiveMemoryWarning:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)applicationDidEnterBackground:(NSNotification *)notification {
[self dispatchAppEventWithName:@"ApplicationDidEnterBackground" body:nil];
}
- (void)applicationWillEnterForeground:(NSNotification *)notification {
[self dispatchAppEventWithName:@"ApplicationWillEnterBackground" body:nil];
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
[self dispatchAppEventWithName:@"ApplicationDidFinishLaunching" body:nil];
}
- (void)applicationDidBecomeActive:(NSNotification *)notification {
[self dispatchAppEventWithName:@"ApplicationDidBecomeActive" body:nil];
}
- (void)applicationWillResignActive:(NSNotification *)notification {
[self dispatchAppEventWithName:@"ApplicationWillResignActive" body:nil];
}
- (void)applicationDidReceiveMemoryWarning:(NSNotification *)notification {
[self dispatchAppEventWithName:@"ApplicationDidReceiveMemoryWarning" body:nil];
}
- (void)dispatchAppEventWithName:(NSString *)name body:(NSDictionary *)body {
[self.bridge.eventDispatcher sendAppEventWithName:name body:body];
}
@end
我知道這些事件和通知已經在RCTAppState
可用,但我正在嘗試自己推出一個解決方案來學習:
我嘗試過使用兩者:
[self.bridge.eventDispatcher sendAppEventWithName:name body:body];
和:
[self.bridge.eventDispatcher sendDeviceEventWithName:name body:body];
沒有任何成功。 除了用於JavaScript端調度事件的接收者類之外,我不確定這兩種方法之間的區別。
這是相關的React Native代碼:
var {
AppRegistry,
StyleSheet,
Text,
View,
TabBarIOS,
NativeAppEventEmitter,
} = React;
// ... other code omitted for brevity ...
componentDidMount: function() {
console.log("main component didMount");
debugger;
NativeAppEventEmitter.addListener("ApplicationDidFinishLaunching", (reminder) => { console.log("app event emitter: ApplicationDidFinishLaunching:", reminder)} );
},
調試器行在Chrome調試器中命中, NativeAppEventEmitter
undefined
,而是:
NativeAppEventEmitter
EventEmitter {_subscriber: EventSubscriptionVendor}_subscriber:
EventSubscriptionVendor__proto__: EventEmitter
我的猜測是我做錯了什么,但我不知道是什么。 任何幫助將不勝感激。
訂閱事件的代碼和發送事件的代碼很好。
您遇到的問題是因為ApplicationDidFinishLaunching
事件在您訂閱事件的componentDidMount
之前觸發。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.