React-native in the development engineering also has many functions which can not achieve, this needs to use the native application to realize, React-native realizes and the iOS native interaction only needs to carry on the following several steps.
1, Import rctbridgemodule header file
#import <React/RCTBridgeModule.h>
2, the introduction of the agreement
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
@interface nativetest: Nsobject<rctbridgemodule>
@end
3. Export modules and methods
#import "NativeTest.h"
@implementation nativetest
//export module, default to this class name
Rct_export_module () without adding parameters;
Export method, the method of bridging to JS must return a value type of void
Rct_export_method (dosomething: (NSString *) teststr) {
NSLog (@ "%@ ===> DoSomething ", teststr);
}
@end
4, in the react-native call
Create native module
var nativetest = require (' react-native '). Nativemodules.nativetest;
Method calls
nativetest.dosomething (' ZW name ');
The steps for react-native to call iOS native methods are complete. a method with a callback
1, Response
#pragma mark-response
///Export method, bridge to JS method return value type must be void
//callback parameter must be two, the first is the state, the second is the parameter/*
Rct_export_method ( DoSomething: (NSString *) teststr resolver: (Rctresponsesenderblock) callback) {
NSLog (@ "%@ ===> dosomething") TESTSTR);
NSString *callbackdata = @ "callback data"; Prepare the callback to return the data
callback (@[[nsnull Null],callbackdata]);
}
Call Method:
ClickAction () {
//Response invocation mode
//Create native module
var nativetest = require (' react-native '). Nativemodules.nativetest;
Method invokes the
nativetest.dosomething (' rn-> native Data '), (error,events) => {
if (error) {
Console.warn ( error);
} else {
alert (events)/returned data
}
});
2, Promise
#pragma mark-promise
///Export method, bridge to JS method return value type must be void
/* has two callbacks, one for the correct, one for error*/
Rct_remap_method ( Testpromisesevent,name: (NSString *) teststr
resolver: (rctpromiseresolveblock) Resolve Rejecter
: ( Rctpromiserejectblock) reject
{
NSString *promisesdata = @ "promises data";//Prepare callback back Data
if (promisesdata) {
Resolve (TESTSTR);
} else {
nserror *error=[nserror errorwithdomain:@ "I am promise callback error message ..." code:101 Userinfo:nil];
Reject (@ "no_events", @ "There were no events", error);
}
Call Method:
ClickAction2 () {
//Promise invocation mode
//Create native module
var nativetest = require (' react-native '). Nativemodules.nativetest;
Method calls
nativetest.thepromisestest ((' ZW ')). Then ((events) =>{
alert (events+1111)
});