I have successfully resolved the issue
Caution: Please be advised that this solution involves using a deprecated method in react native implementation. I encountered difficulties trying to inherit from RCTEventEmitter
and send an event as intended, resulting in the _bridge
variable becoming nil
every time.
Make sure Swift is properly connected to Objective C (in case you're utilizing swift for sending events to JavaScript)
Do not create instances of exported Native modules (whether written in Swift or Objective C)
Instead, let React Native's underlying structure handle this by exporting specific Native Class Objective C Implementation code or Swift code (the Native Module) to React-Native for each class that needs to send an event. This setup allows JavaScript to listen for the event.
var publicBridgeHelperInstance = PublicBridgeHelper() //create an instance of the objective c class inside the .swift file for future use in obtaining a reference to the bridge required for sending events to JavaScript in react native
@objc(DeviceManager) //export swift module to objective c
class DeviceManager: NSObject {
@objc(deviceDidComeOnline:) //expose the function to objective c
public func deviceDidComeOnline(_ device: GCKDevice) {
//assume this deviceDidComeOnline function is triggered independent of javascript, possibly from native code or even a native button click to verify functionality...
//emit an event to a javascript function within a React Native Component listening for the event like so:
//1. obtain the bridge reference to facilitate event transmission from Native to Javascript in React Native (custom code involved here to make this operation functional)
let rnBridge = publicBridgeHelperInstance.getBridge() //fetches the bridge stored in AppDelegate.m via the `rootView.bridge` attribute (details on this below)
//(if needed, confirm bridge availability with a print statement:
print("rnBridge = \(rnBridge)")
//2. actually dispatch the event through the eventDispatcher
rnBridge?.eventDispatcher().sendAppEvent(withName: "test", body: "testBody data!!!")
}
}
In AppDelegate.h
, add (in addition to existing code present in the file)
#import "YourProjectsBridgingHeaderToMakeThisCodeAvailableInSwift.h" //replace this with your actual header created when integrating a swift file (refer to guides if unsure how to connect swift to objective c)
@interface PublicBridgeHelper: NSObject
-(RCTBridge*)getBridge;
@end
In AppDelegate.m
, include (alongside existing content in the file)
#import <React/RCTRootView.h>
RCTBridge *rnBridgeFromRootView;
@implementation PublicBridgeHelper //established solely to return rnBridgeFromRootView defined above to my Swift class while sending an event to JavaScript within a react native Component
-(RCTBridge*)getBridge {
NSLog(@"rnBridgeFromRootView = @%@", rnBridgeFromRootView);
return rnBridgeFromRootView;
}
Note: Ensure to append the following line of code to the bridging header file in Objective C .h to enable usage of the PublicBridgeHelper
definition in the .swift code
#import "AppDelegate.h"
lastly,
To illustrate setting up the rnBridgeFromRootView variable utilized in AppDelegate.m (later returned and used in the .swift code before transmitting the event to javascript)
Go into AppDelegate.m
and within the method body of
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { ... }
Add the following after the line that initializes the rootView
variable
e.g., following a line resembling
RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation moduleName:@"YourProjecNameProbably" initialProperties:nil launchOptions: launchOptions];
insert:
rnBridgeFromRootView = rootView.bridge //set the bridge to be exposed, retrieved later, and utilized by the swift class
Explaining the
publicBridgeHelperInstance.getBridge()
segment found in the .swift file
publicBridgeHelper
denotes an instance of an objective c class granting the swift class access to the react native bridge reference
If there are lingering issues comprehending my response, refer to my explanatory video for additional guidance:
https://www.youtube.com/watch?v=GZj-Vm9cQIg&t=9s