I'm trying to get a hang on react native and native modules but I'm stuck for a few days now. I tried with this tutorial: https://teabreak.e-spres-oh.com/swift-in-react-native-the-ultimate-guide-part-1-modules-9bb8d054db03#b780
I want to call the function testfunction() in my Bridge.swift file, when I click a button in my React Native project. The function just needs to print "test" in the xcode console.
Bridge.swift
import Foundation
import UIKit
@objc(Bridge)
class Bridge : NSObject {
@objc
func testfunction(){
print("test")
}
}
Bridge.m
#import "React/RCTBridgeModule.h"
@interface RCT_EXTERN_MODULE(Bridge, NSObject)
RCT_EXTERN_METHOD(testfunction)
@end
Playground-Bridging-Header.h
#import "React/RCTBridgeModule.h"
Snippet of the button in App.js
<TouchableOpacity
style={styles.button}
onPress={() => NativeModules.Bridge.testfunction()}>
<Text>TEST</Text>
</TouchableOpacity>
When I click the button, nothing is printed in the console.
Sidenote: My project is called Playground.
It has a Today Widget target called PlaygroundWidget. The Bridge.swift file is located in this PlaygroundWidget folder, this is because this file will contain methods that will be called from the TodayViewController for the widget.
I needed to put the Bridge.m file in the Playground folder otherwise it couldn't resolve "React/RCTBridgeModule.h".
I'm using React Native 0.61 and xCode 11.1