1, the direct use of nativemodules components, "mycustommodule" is actually our corresponding OC engineering class, "Processstring" is the method in the class
/** * Call iOS Module 1 * http://www.cnblogs.com/daomul/*/' Use strict ';varReact = require (' react-native '));var{Nativemodules, View, text,scrollview,stylesheet,touchableopacity} =React;varMessage =React.createclass ({getinitialstate () {return{text: ' Goodbye world '. }; }, Componentdidmount () { NativeModules.MyCustomModule.processString( This. State.text, (text) = = { This. SetState ({text}); }); }, Render:function() { return ( <scrollview style={styles.view}> <text>{ This.state.text}</text> </ScrollView> ); },});varStyles =stylesheet.create ({view:{margin:20,},}); React.AppRegistry.registerComponent (' Hellworld ', () = Message);
So the implementation of the corresponding OC project: through the Rct_export_module ()-like JS can open the OC API, communication with the call
#import<Foundation/Foundation.h>#import "RCTBridgeModule.h"@interfaceMycustommodule:nsobject <RCTBridgeModule>@end#import "MyCustomModule.h"@implementationMycustommodulerct_export_module (); Rct_export_method (processstring: (NSString*Input callback: (Rctresponsesenderblock) callback) {Callback (@[[input stringbyreplacingoccurrencesofstring: @"Goodbye"Withstring:@"Hello"]]);}@end
2, we can also introduce Nativemodules way, "CalendarView" corresponding to the OC class name, Addeventwithname is the method name,
var Calendarmanager = require (' Nativemodules '). CalendarView; Calendarmanager.addeventwithname (' Something need to remmber ', ' Shenzheng Nanshan ');
If there are many parameters we would like to pass the data through the nsdictionary, it is also possible, the OC side can do the processing of convert:
Calendarmanager.addeventwithname (' Something need to Remmber ', { ' ShenZhen Nanshan ', ' Other Thingslo '});
Co-terminal code:
#import "CalendarVIew.h"#import "RCTConvert.h"#import "RCTLog.h"@implementationCalendarviewrct_export_module (); Rct_export_method (addeventwithname: (NSString*) name DIC: (Nsdictionary *) (DIC) {NSString*LOCALSTR = [Rctconvert nsstring:dic[@" Location"]]; NSString*DESCSTR = [Rctconvert nsstring:dic[@"Description"]]; Rctloginfo (@"The name is%@,and the location is%@,%@", NAME,LOCALSTR,DESCSTR);}
React native--the way to call Native modules