1

I'm writing a Native Module for React Native and need to return a value in a custom data type to React Native. To return a value I use the Promise method like this:

// .swift
    
@objc
func getCardType(
  _ cardNumber: NSString,
  resolver resolve: RCTPromiseResolveBlock,
  rejecter reject: RCTPromiseRejectBlock
) {
  let cardType = cardUtils.getTypeOf(cardNumber: String(cardNumber))
  resolve(cardType)
}

and expose it to React Native like this:

// .m

#import "React/RCTBridgeModule.h"

@interface RCT_EXTERN_MODULE(CheckoutBridge, NSObject)

RCT_EXTERN_METHOD(getCardType:(NSString)cardNumber
  resolver:(RCTPromiseResolveBlock)resolve
  rejecter:(RCTPromiseRejectBlock)reject
)

@end

The type of cardType is CardType which is defined by a 3rd party library When calling the function from React Native, I receive a null value, even though I can see the value on the Native side actually being CardType(scheme: .visa, name: ...).

I figured this happens because cardType is not one of the supported argument types, and realised that it might be possible to use RCTConvert to somehow allow CardType values to being sent over the bridge. I had a hard time finding an online reference to how to do this, especially when wanting to do it with Swift. Any Ideas?

Wiingaard
  • 4,150
  • 4
  • 35
  • 67

0 Answers0