美文网首页
iOS调用ReactNative方法

iOS调用ReactNative方法

作者: 代码堆在那_我往哪栈 | 来源:发表于2019-10-31 15:52 被阅读0次
    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 <Foundation/Foundation.h>
      #import <React/RCTBridgeModule.h>
      #import <React/RCTEventEmitter.h>
      @interface ReactInteraction : RCTEventEmitter <RCTBridgeModule>
      + (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<NSString *> *)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方法

          本文链接:https://www.haomeiwen.com/subject/iqjtbctx.html