Search code examples
javascriptswiftreact-nativepromisenative-module

ReactNative native-modules Promise passing field and in Swift


I'm trying to run a promise in React Native using Swift and passing one field (array). This is the code that i have:

GuidedTourInterfaceBridge.m

@interface RCT_EXTERN_MODULE(GuidedTourInterface, NSObject)

   RCT_EXTERN_METHOD(readFile(_ arr: NSArray, resolver: RCTPromiseResolveBlock resolve, rejecter reject: RCTPromiseRejectBlock reject))
@end

GuidedTourInterface.swift:

...
@objc
func readFile(_ arr: NSArray, resolver resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) -> Void {
    ...
    resolve(["success" : true, "base64Content" : base64Content ])
}
...

JS File:

...
readFileInfoJson = async () => {
  try {

      var result = await guidedTourInterface.readFile([this.props.mapToShow+"/info.json"]);
      ...
   } catch (e) {
      console.error(e);
   }
 }

The error message I am receiving:

guidedTourInterface.readFile is not a function. (In 'guidedTourInterface.readFile([_this.props.mapToShow+"/info.json"])', 'guidedTourInterface.readFile' is undefined)]

Do you have any suggestions how to solve this or what I am doing wrong? Thanks in advance


Solution

  • I was able to figure out how to get resolve the problem. Here is the solution for anyone who might need

    GuidedTourInterfaceBridge.m

    RCT_EXTERN_METHOD(readFile:(NSArray *)arr resolve:(RCTPromiseResolveBlock *)resolve reject:(RCTPromiseRejectBlock *)reject)
    

    GuidedTourInterface.swift:

      func readFile(_ arr: NSArray, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
    

    JS File:

    var result = await guidedTourInterface.readFile([this.props.mapToShow+"/info.json"])
              .then((result) => {