iOS调用ReactNative方法

RCTRootView *rootView =
[[RCTRootView alloc] initWithBundleURL: jsCodeLocation
                            moduleName: @"ptm"
                     initialProperties:nil
                         launchOptions: nil];
JLBReactNativeBridgeController *vc = [[JLBReactNativeBridgeController alloc] init];
vc.view = rootView;

[[ReactInteraction shareInstance] setValue:rootView.bridge forKey:@"bridge"];
[[ReactInteraction shareInstance] init:@"qwer"];

[self.navigationController pushViewController:vc animated:YES];

必须调用这个方法,注册bridge,不然后面的调不起来。
[[ReactInteraction shareInstance] setValue:rootView.bridge forKey:@"bridge"];

不能写在JLBReactNativeBridgeController的viewDidLoad方法里面。必须在前一个控制器里面直接写。
[[ReactInteraction shareInstance] init:@"qwer"];

ReactInteraction.h文件

  #import 
  #import 
  #import 
  @interface ReactInteraction : RCTEventEmitter 
  + (instancetype)shareInstance;
  - (void)init:(NSString *)parameter;
  - (void)login;
  @end

ReactInteraction.m文件

#import "ReactInteraction.h"

#define INIT @"EventInit"
#define LOGIN @"EventLogin"

@implementation ReactInteraction

static ReactInteraction *instance = nil;

RCT_EXPORT_MODULE();
+ (instancetype)shareInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (NSArray *)supportedEvents
{
    return @[INIT,LOGIN];
}

RCT_EXPORT_METHOD(init:(NSString *)msg) {
    [self.bridge enqueueJSCall:@"RCTDeviceEventEmitter"
                        method:@"emit"
                          args:@[INIT, msg]
                    completion:NULL];
    
    
}

RCT_EXPORT_METHOD(login) {
    [self.bridge enqueueJSCall:@"RCTDeviceEventEmitter"
                        method:@"emit"
                          args:@[LOGIN]
                    completion:NULL];
}

@end

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