ReactNative-调用iOS原生方法

React native 调用原生接口实现起来比较简单,自定义接口类PushManager,需要实现RCTBridgeModule协议,导入RCT_EXPORT_MODULE宏定义.

#import 
#import 
#import 

@interface PushManager : NSObject

@end
#import "PushManager.h"

@implementation PushManager

RCT_EXPORT_MODULE();

- (NSDictionary *)baseInfomation {
    return @{ @"name": @"FlyElephant" };
}

RCT_EXPORT_METHOD(showMessage:(NSString *)message type:(NSString *)type)
{
    RCTLogInfo(@"FlyElephant 本地调用 %@---%@---", message, type);
}

@end

React native导入本地原生模块:

import {
    Text,
    View,
    TouchableHighlight,
    NativeModules,
    NativeEventEmitter,
} from 'react-native';

const PushManager = NativeModules.PushManager;

调用原生模块方法:

 PushManager.showMessage("消息推送","local");

原生模块还支持回调函数的调用,iOS提供回调方法:

RCT_EXPORT_METHOD(blockCallbackEvent:(RCTResponseSenderBlock)callback)
{
    NSArray *events=@[@"Callback ", @"Block ", @"FlyElephant"];
    callback(@[[NSNull null],events]);
}

JavaScript回调处理:

                            PushManager.blockCallbackEvent((error, events) => {
                                if (error) {
                                    console.error(error);
                                } else {
                                    console.log("回调结果:" + events);
                                }
                            });

你可能感兴趣的:(ReactNative-调用iOS原生方法)