How to add a Swift Code in a React native library?

Viewed 531

I have created a React native library. In the native iOS code in it, I had imported a Swift framework. When I import this React library in another project and I call the showToast method I get this error: Exception 'showToast: is not a recognized Objective-C method'

I got my Codes below if it helps;

here is my RNToastLib.m:

#import "RNToastLib.h"
#import <Foundation/Foundation.h>

@implementation RCT_EXTERN_MODULE(ToastManager, NSObject)

RCT_EXTERN_METHOD(showToast:(NSString *)text)

@end

here is my RNToastLib.h:

#if __has_include("RCTBridgeModule.h")
#import "RCTBridgeModule.h"
#else
#import <React/RCTBridgeModule.h>
#endif

@interface RNToastLib : NSObject <RCTBridgeModule>
@end

here is my ToastManager.Swift:

import Foundation
import Toaster

@objc(ToastManager)
class ToastManager: NSObject {

  @objc func showToast(_ text: String) {
    Toast(text: text, duration: Delay.short).show();
  }
}

here is my RNToastLib-Bridging-Header.h:

#import "RCTBridgeModule.h"

and here is my index.js:

import {NativeModules} from 'react-native';

const {ToastManager} = NativeModules;

export default class ToastLib {

    static showToast(text) {
        ToastManager.showToast(text);
    }
}
0 Answers
Related