React Native - 在AppDelegate(iOS)中将事件从Native发送到JavaScript [英] React Native - Sending events from Native to JavaScript in AppDelegate (iOS)

查看:468
本文介绍了React Native - 在AppDelegate(iOS)中将事件从Native发送到JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的React本机应用程序中,我正在尝试将本地代码中的事件发送到AppDelegate中的JavaScript。要做到这一点,我打电话:

In my React native app, I am trying to send events from Native Code to JavaScript in the AppDelegate. To do this I call:

[self.bridge.eventDispatcher sendAppEventWithName:@"EventReminder"
                                           body:@{@"name": eventName}];

在我的应用代表中。当然要做到这一点我需要导入:

In my app delegate. Of course to do this I need to import:

并合成桥梁

@synthesize bridge = _bridge;

但是此后的事件,桥变量不存在。为了使这个错误消失,我让我的AppDelegate符合RCTBridgeModule协议,如下所示:

But event after this, the bridge variable doesn't exist. To make this error go away I made my AppDelegate conform to the RCTBridgeModule protocol like so:

AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeModule>

然后在我的AppDelegate.m中,我做了:

And then in my AppDelegate.m, I did:

RCT_EXPORT_MODULE()

毕竟我的桥终于没有错误,但每次我在AppDelegate中使用它,它都是零。

After all that my bridge is finally not erroring, but Everytime I use it in the AppDelegate, it is nil.

我哪里出错?

提前致谢。

推荐答案

RCTBridge在启动时创建每个模块类的新实例,所以当你将您的AppDelegate导出为桥接模块,您告诉桥梁创建一个新的AppDelegate,并为其提供一个桥接实例。

RCTBridge creates new instances of each module class when it starts up, so when you export your AppDelegate as a bridge module, you're telling the bridge to create a new AppDelegate, and give it a bridge instance.

iOS还会创建一个AppDelegate实例您的应用程序启动,但iOS创建的实例与RCTBridge创建的实例不同。

iOS also creates an instance of AppDelegate when your application launches, but the instance created by iOS isn't the same instance created by RCTBridge.

所以基本上,你有两个AppDelegate实例:你是一个试图访问self.bridge,这不是由RCTBridge创建的,因此没有对它的引用,以及由RCTBridge,它有一个桥,但不是你的UIApplication的代表,并且没有运行你的代码。

So basically, you have two instances of AppDelegate: the one you're trying to access self.bridge from, which wasn't created by RCTBridge and so doesn't have a reference to it, and the one created by RCTBridge, which has a bridge, but isn't the delegate for your UIApplication, and isn't running your code.

你有几个选择:

1)当您使用RCTBridgeDelegate的extraModules方法创建AppDelegate实例时,可以将其传递到桥中。这可以告诉桥接器使用模块的现有实例,而不是创建一个新模块。

1) you could pass your AppDelegate instance into the bridge when you create it by using the RCTBridgeDelegate's extraModules method. This lets you tell the bridge to use an existing instance of a module, instead pf creating a new one.

2)您可以通过RCTRootView访问网桥而不是制作你的AppDelegate进入一个模块,以便给它一个self.bridge属性。

2) you could access the bridge via your RCTRootView instead of making your AppDelegate into a module so that it gets given a self.bridge property.

3)将需要通过桥接的逻辑移出AppDelegate进入一个新模块。如果它需要由AppDelegate中的事件触发,请使用NSNotifications与模块实例通信(我们将此模式用于RCTPushNotificationManager)。

3) Move the logic that needs to talk to the bridge out of AppDelegate into a new module. If it needs to be triggered by an event inside AppDelegate, use NSNotifications to communicate with the module instance (we use this pattern for RCTPushNotificationManager).

这些选项中,

选项1)可能是最复杂的做法。

Option 1) is probably the most complicated to do correctly.

选项2)可能是最容易做到的你可能已经在你的AppDelegate中有一个RCTRootView实例,你可以参考。

Option 2) is probably the easiest to do because you presumably already have an instance of RCTRootView in your AppDelegate that you can reference.

选项3)从技术角度看是理想的,因为它可以防止你意外发送事件网桥已正确初始化(可能会崩溃,或出现意外行为)。

Option 3) is ideal from a technical perspective, because it prevents you from accidentally sending events before the bridge is properly initialized (which might crash, or behave unexpectedly).

这篇关于React Native - 在AppDelegate(iOS)中将事件从Native发送到JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆