I am creating native module using create-react-native-module with swift. After that I have followed react native official documentation for iOS setup. doc link:: https://facebook.github.io/react-native/docs/native-modules-ios
I have created create-react-native-module with example. I am just adding simple function with returning string "Hello World" inside my native module.
My 'CustomModule-Bridging-Header.h'::
#import <React/RCTBridgeModule.h>
My 'CustomModule.m'::
#import <React/RCTBridgeModule.h>
@interface RCT_EXTERN_MODULE(CustomModule, NSObject)
RCT_EXTERN_METHOD(sampleMethod)
+ (BOOL) requiresMainQueueSetup {
return YES;
}
@end
My 'CustomModule.swift':
import Foundation
@objc(CustomModule)
class CustomModule: NSObject {
@objc(sampleMethod)
func sampleMethod() -> String {
return "Hello World"
}
}
After making these changes to native module I have installed dependencies again inside example. Now my App.js is look like::
import React, { Component } from 'react';
import { Platform, Text, View } from 'react-native';
import CustomModule from 'react-native-custom-module';
export default class App extends Component {
state = {
message: '--'
};
async componentDidMount() {
const msg = await CustomModule.sampleMethod();
console.log('javascript calling msg::', msg);
this.setState({
message: msg
});
}
render() {
return (
<View>
<Text>CustomModule example☆</Text>
<Text>{this.state.message}</Text>
</View>
);
}
}
Here I am getting "msg" value as undefined. Please help!