wheresmycookie
wheresmycookie

Reputation: 773

React Native Bridge not returning correct value to javascript

I'm trying to build a native module to use AVFoundation. I need to write a function in Swift that returns a value to javascript, so I have followed the instructions on Exporting Swift.

Here are my 3 resulting files:

MediaManager.swift

@objc(MediaManager)
class MediaManager: NSObject {

  @objc(getFrame)
  func getFrame() -> Int {
    return 123;
  }
}

MediaManagerBridge.m

#import <React/RCTBridgeModule.h>

@interface RCT_EXTERN_MODULE(MediaManager, NSObject)

RCT_EXTERN_METHOD(getFrame)

+ (BOOL)requiresMainQueueSetup
{
  return NO;
}

@end

Bridging-Header.h

#import <React/RCTBridgeModule.h>

When I run console.log(MediaManager.getFrame()), I get "undefined". When I run console.log(MediaManager.getFrame), I get:

ƒ fn() {
        for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
          args[_key2] = arguments[_key2];
        }

        var lastArg = args.length …

which is definitely not the function that I exported. I'm finding it a bit difficult to parse the docs - what have I done wrong here?

Upvotes: 1

Views: 628

Answers (1)

Sharif Mahmoud
Sharif Mahmoud

Reputation: 107

The return value is only for constants.

Use this

  func getFrame(_ callback: RCTResponseSenderBlock) {
callback([123])

}

and in react native do this

MediaManager.getFrame(
      (value) => console.log(value),
    )

Upvotes: 1

Related Questions