Reputation: 6918
It seems the react native have the ability to integrate iOS project and using it as the normal react component, but I have read the official document and find there is no complete demo, is there a simple way to show how to do it begin to the end?
Upvotes: 1
Views: 1482
Reputation: 3604
What you're looking for is React Native Modules
https://facebook.github.io/react-native/docs/native-modules-ios
In the documentation they have the following example which you can follow along with and implement, then modify to fit your needs.
// CalendarManager.h #import <React/RCTBridgeModule.h> @interface CalendarManager : NSObject <RCTBridgeModule> @end
// CalendarManager.m #import "CalendarManager.h" #import <React/RCTLog.h> @implementation CalendarManager RCT_EXPORT_MODULE(); RCT_EXPORT_METHOD(addEvent:(NSString *)name location:(NSString *)location) { RCTLogInfo(@"Pretending to create an event %@ at %@", name, location); }
This example doesn't really do anything, it just created a method called CalendarManager.addEvent
that you can call from React Native that will console log some stuff using RCTLog
.
.h file
- The header file that connects iOS to React Native, pretty much nothing goes in here except what's shown (changed to match your module name).m file
- The module file that defines your methods that you call in JavaScript in which can call your SDK methods.@implementation CalendarManager
- This is where you name your module, so you an import it in ReactNative with NativeModules.CalendarManager
RCT_EXPORT_MODULE()
- Defines that this file is a React Native moduleRCT_EXPORT_METHOD()
- Defines a method you can call from React Native. You can have as many of these as you want.Inside your RCT_EXPORT_METHOD()
definition, you can call your SDK with whatever parameters you pass it.
Talking of parameters, there are different types that are defined as follows:
- string (NSString)
- number (NSInteger, float, double, CGFloat, NSNumber)
- boolean (BOOL, NSNumber)
- array (NSArray) of any types from this list
- object (NSDictionary) with string keys and values of any type from this list
- function (RCTResponseSenderBlock)
Upvotes: 2