I'm currently working on creating a native swift module that includes a function I need to call in my javascript code. At the moment, I have three key files for this setup: module.swift
, module.m
, and the bridging header.
This is what MyModule.swift looks like:
import Foundation
@objc(MyModule)
class MyModule: NSObject {
@objc func callbackMethod(callback: RCTResponseSenderBlock) -> Void {
let resultsDict = [
"success" : true
];
callback([NSNull() ,resultsDict])
}
}
Now, let's take a look at MyModule.m:
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
@interface RCT_EXTERN_MODULE(MyModule, NSObject)
RCT_EXTERN_METHOD(callbackMethod:(RCTResponseSenderBlock)callback)
@end
Here's a snippet from the bridging header:
#import React/RCTBridgeModule.h
#import React/RCTEventEmitter.h
To make use of this functionality in JavaScript, here's how you can call it:
const { MyModule } = require('NativeModules');
MyModule.callbackMethod((err,r) => console.log(r));
The issue that arises upon running the project stems from the error message:
Exception 'callbackMethod:(RCTResponseSenderBlock)callback is not a recognized Objective-c method.'
Despite following the documentation closely, I seem to be facing this problem. Any assistance or insights would be greatly appreciated!