React native--the way to call Native modules

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.