react-native icon indicating copy to clipboard operation
react-native copied to clipboard

Is there a way to emit an event from a Objective-C class method?

Open BartoszKlonowski opened this issue 3 years ago • 1 comments

Description

I'm looking for a way to call sendEventWithName from a class method of an interface that inherits from RCTEventEmitter.

Reason: I want to send an event to JS from any place of Objective-C native code of my app using a native module. So the scenario would be:

@interface LocalEventEmitter : RCTEventEmitter<RCTBridgeModule>
- (NSArray<NSString*>*)supportedEvents;
+ (void)emit:(NSString*)data;
@end

and in the emit method I would like to emit the event.

I'm using the class method so I can call it from anywhere, without creating the instance as it would require some React Native context that I don't have.

However, I'm stuck at:https://github.com/facebook/react-native/blob/a70354df12ef71aec08583cca4f1fed5fb77d874/React/Modules/RCTEventEmitter.m#L53-L61

I know the reasoning behind this, but I can't find a way to somehow "keep" the context of React Native and use it in my class method to correctly send the event.

So my question is: How to emit the event from iOS native module using class method?

Version

0.69.1

Output of npx react-native info

System: OS: macOS 12.4 CPU: (10) arm64 Apple M1 Pro Memory: 1.74 GB / 32.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 18.4.0 - ~/.nvm/versions/node/v18.4.0/bin/node Yarn: 1.22.18 - /opt/homebrew/bin/yarn npm: 8.12.1 - /opt/homebrew/bin/npm Watchman: 2022.07.04.00 - /opt/homebrew/bin/watchman Managers: CocoaPods: 1.11.3 - /opt/homebrew/bin/pod SDKs: iOS SDK: Platforms: DriverKit 21.4, iOS 15.5, macOS 12.3, tvOS 15.4, watchOS 8.5 Android SDK: API Levels: 28, 29, 30, 31, 32 Build Tools: 28.0.3, 29.0.2, 30.0.2, 30.0.3, 31.0.0, 32.0.0, 32.1.0 System Images: android-28 | Intel x86 Atom, android-28 | Intel x86 Atom_64, android-28 | Google Play Intel x86 Atom, android-28 | Google Play Intel x86 Atom_64, android-29 | Intel x86 Atom_64, android-29 | Google APIs Intel x86 Atom, android-30 | Google APIs Intel x86 Atom_64, android-31 | Android TV ARM 64 v8a, android-32 | Google APIs ARM 64 v8a Android NDK: 21.3.6528147 IDEs: Android Studio: 2021.1 AI-211.7628.21.2111.8139111 Xcode: 13.4.1/13F100 - /usr/bin/xcodebuild Languages: Java: 11.0.11 - /usr/bin/javac npmPackages: @react-native-community/cli: Not Found react: 18.0.0 => 18.0.0 react-native: 0.69.1 => 0.69.1 react-native-macos: Not Found npmGlobalPackages: react-native: Not Found

Steps to reproduce

  1. Create the app
  2. Implement basic native module on both Android and iOS app that is to emit events to JS
  3. For Android keep the reactContext in a static variable to use it for emitting event
  4. For iOS send the event from a class method

Snack, code example, screenshot, or link to a repository

@implementation LocalEventEmitter

RCT_EXPORT_MODULE()

- (NSArray<NSString*>*)supportedEvents
{
    return @[@"newDataReceived"];
}

+ (void)emit:(NSString*)data
{
    [[[self alloc] init] sendEventWithName:@"newDataReceived" body:@{@"summary": data}];
}
@end

BartoszKlonowski avatar Jul 24 '22 21:07 BartoszKlonowski

also I want supprt

  • (NSArray<NSString *> *)supportedEvents

to use external arguments (such as viewcontroller.hash or uuid) but now can not set it. and now NativeEventEmitter.addListener with same eventType only first got callback

krmao avatar Aug 24 '22 07:08 krmao

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Feb 21 '23 00:02 github-actions[bot]

Let me bump it then, please. I would still use some advise regarding this.

BartoszKlonowski avatar Feb 22 '23 19:02 BartoszKlonowski

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Jan 16 '24 05:01 github-actions[bot]

This issue was closed because it has been stalled for 7 days with no activity.

github-actions[bot] avatar Feb 09 '24 05:02 github-actions[bot]