IOS Native调用React Native平台

需求:由IOS原生端主动发起调用,传递参数给RN平台。

一、IOS端

1、.h文件

#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
#import <React/RCTBridge.h>
@interface ReactInteraction : RCTEventEmitter <RCTBridgeModule>
- (void)notifyModeChange:(NSString*) result;
+(id)allocWithZone:(NSZone *)zone ;
@end

2、.m文件

//  ReactInteraction.m
//  Neurmt
//  Created by xzy on 2020/3/30.
//  Copyright © 2020 xue. All rights reserved.
//

#import "ReactInteraction.h"
@implementation ReactInteraction

RCT_EXPORT_MODULE();

//.m文件
+(id)allocWithZone:(NSZone *)zone {
  static ReactInteraction *sharedInstance = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    sharedInstance = [super allocWithZone:zone];
  });
  return sharedInstance;
}

- (NSArray<NSString *> *)supportedEvents
{
  return @[@"EventReminder",@"EventWeiXinPayFinish",@"currentMode"];
}

- (void)notifyModeChange:(NSString*) result
{
    [self sendEventWithName:@"currentMode" body:@{@"result": result}];
}
@end

3、bridge赋值

rootView = [[RCTRootView alloc]initWithBridge:[MallBridgeAPI shareInstance]
                                           moduleName:@"rmtApp"
                                    initialProperties:props];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(clickBack) name:@"openHomePage" object:(nil)];
        NSLog(@"Neurmt:结束调用Bundle");
    }
      [ReactInteraction allocWithZone: nil].bridge = rootView.bridge;

4、调用

  ReactInteraction * reactInteraction=[[ReactInteraction alloc] init];
  [reactInteraction notifyModeChange:@"dark"];

二、RN端

import {
    ***
    NativeModules,
    NativeEventEmitter
} from 'react-native';
…
const eventEmitter = new NativeEventEmitter(ReactInteraction);
…
eventEmitter.addListener('currentMode',this.colorModeChange);
  }
…
colorModeChange = (result)=> {
       if (Platform.OS === 'android') {
        …
        }else{
         …
        }
     
  }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值