React-native call iOS Native method

Source: Internet
Author: User
Tags prepare

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)
    });
  
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.