ReactNative-调用iOS原生方法

栏目: IOS · 发布时间: 7年前

内容简介:React native 调用原生接口实现起来比较简单,自定义接口类PushManager,需要实现RCTBridgeModule协议,导入RCT_EXPORT_MODULE宏定义.React native导入本地原生模块:调用原生模块方法:

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

#import 
  
#import
#import

@interface PushManager : NSObject<RCTBridgeModule>

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

作者:FlyElephant

链接:https://www.jianshu.com/p/455e0b32623e


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

High Performance Python

High Performance Python

Andrew Lewis / O'Reilly Media, Inc. / 2010-09-15 / USD 34.99

Chapter 1. Introduction Section 1.1. The High Performance Buzz-word Chapter 2. The Theory of Computation Section 2.1. Introduction Section 2.2. Problems Section 2.3. Models of Computati......一起来看看 《High Performance Python》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具