Event Dispatcher - dispatch event from Swift #672
Replies: 4 comments 2 replies
-
Hey, can you also share |
Beta Was this translation helpful? Give feedback.
-
Hello @atlj , I tried to declare the #ifdef RCT_NEW_ARCH_ENABLED
#import "RNMyModuleSpec.h"
@interface MyModule : NSObject <NativeMyModuleSpec>
- (NSNumber *)multiply:(double)a b:(double)b;
#else
#import <React/RCTBridgeModule.h>
@interface MyModule : NSObject <RCTBridgeModule>
#endif
@end |
Beta Was this translation helpful? Give feedback.
-
Hello @atlj , I am able to call the Objective-C method inside Swift file by commenting the However, by doing that, I am not able to build the project anymore and get the below error
Note: I am using the latest version: {
"create-react-native-library": "0.44.1",
"react-native": "0.76.2"
}, Please let me know if you need anything else for investigetion. Many thanks! |
Beta Was this translation helpful? Give feedback.
-
Hey, I think I have a solution, you're trying to initialize import React // You don't need to rely on objective c to swift bridging header as the React framework already exposes a module
@objc(MySwiftClass)
class MySwiftClass: NSObject {
weak let eventEmitter: RCTEventEmitter // This is weak so we don't prevent the objc module from getting freed
@objc public init(eventEmitter: RCTEventEmitter) { // RCTEventEmitter class is coming from the React module. It's the base class for implementing event emitters
self.eventEmitter = eventEmitter // Hold the reference
}
private func sendEvent(name: String, body: [String, Any]?) { // You can either wrap it inside a function or directly call the eventEmitter.
self.eventEmitter?.sendEvent(
withName: name,
body: body
)
}
// ...
Then on objc side, you have to pass #import "MyModule.h"
#import "my_module-Swift.h"
@implementation MyModule
RCT_EXPORT_MODULE()
// Don't compile this code when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
- (void)hello {
// Call "hello"
MySwiftClass *swiftInstance = [[MySwiftClass alloc] initWithEventEmitter:self];
[swiftInstance hello];
}
// ... You should also make sure your base objc class is extending #import <React/RCTEventEmitter.h>
@interface MyModule : RCTEventEmitter <NativeMyModuleSpec> // Use RCTEventEmitter as the base class instead of NSObject This comment mostly focused on the Swift side, if you want to see a good example, please take a look at this awesome project: https://github.com/birdofpreyru/react-native-static-server/blob/254ad114ee117196be2f69ee68c6f23a8dbf4e62/ios/ReactNativeStaticServer.mm |
Beta Was this translation helpful? Give feedback.
-
Hi everyone, I follow this link and able to create an event in both Android and iOS using the new architecture.
The function to dispatch an event:
emitOnValueChanged
However, from my previous project, I have a location tracking feature written in Swift and will send events to JS code.
Instead of re-write the code in Objective-C, I decide to call
emitOnValueChanged
method inside Swift and currently get stuck at it.Below is the code and how I set it up. To be short, the below files only contain some very basic code so we can investigate in why Objective-C methods are not available in MyModule.swift.
I think if I can call one Objective-C method inside MyModule.swift, I can do the same with the
emitOnValueChanged
Step 1: Create a sample library
Step 2: Add
hello
methodNativeMyModule.ts
index.tsx
App.tsx
MyModule.swift
my-module-Bridging-Header.h
MyModule.mm
I am able to call and print the "Hello from Swift" message
Step 3: Call
multiply
method of Objective-C inside MyModule.swiftMyModule.swift
Please let me know if you have any suggestion.
Many thanks!
Beta Was this translation helpful? Give feedback.
All reactions