Say I have the following Swift class:
@objc(ExampleClass)
class ExampleClass: NSObject {
init() {}
@objc func exampleMethod(_ message: String, _ properties: [String: Any]? = nil) -> Void {}
}
And the following Header:
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
@interface RCT_EXTERN_MODULE(ExampleClass, NSObject)
RCT_EXTERN_METHOD(exampleMethod:(NSString *)name (NSDictionary *)properties)
@end
Which I then call with the following React Native code:
import { NativeModules } from 'react-native'
NativeModules.ExampleClass.exampleMethod('example', {'hello': 'world'})
This results in the following error message:
ExceptionsManager.js:73 Exception 'exampleMethod: is not a recognized Objective-C method.' was thrown while invoking trackEvent on target SegmentTracker with params (
"example",
{
hello = world;
}
)
While calling it like so:
NativeModules.ExampleClass.exampleMethod('example')
results in:
ExceptionsManager.js:73 Exception 'exampleMethod: is not a recognized Objective-C method.' was thrown while invoking trackEvent on target SegmentTracker with params (
"example"
)
I have two questions: 1) How do I pass JSON from React Native to Swift? 2) How do I correctly allow for optional parameters in RCT_EXTERN_METHOD? And if that's not possible, how do I handle the case where I would like to sometimes pass a second parameter, and sometimes not?