RN和IOS原生端交互

1.RCTBridgeModule

#import <Foundation/Foundation.h>
#import "RCTBridgeModule.h"
#import <React/RCTBridgeModule.h>
@interface IOSHandler : NSObject <RCTBridgeModule>

@end

//~~~~~~~~~~~~~~~~~~~~~~~~~
#import "IOSHandler.h"
@implementation IOSHandler
RCT_EXPORT_MODULE();
//goPage 名称 ,astring 参数  resolver 成功回调 reject 失败回调
//多个调用需要修改 astring 参数为其他名称,否则会报错
RCT_REMAP_METHOD (goPage,
                  astring:(NSString *) str
                  resolver:(RCTPromiseResolveBlock)resolver
                  rejecter:(RCTPromiseRejectBlock)reject) {
  NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
  id goPageTag = [userDefaults  objectForKey:@"skip_url"];
  if (goPageTag!=nil) {
    NSString * temp = goPageTag;
    if([temp isEqualToString:@"ZYRCB"]) {
      temp =@"B2C";
    } else if ([temp isEqualToString:@"ZYO2O"]) {
      temp = @"O2O";
    }
    resolver(@{@"START_FLAG":@"ZYRCB",@"flag": temp});
    
  }else {
    reject(@"400",@"",nil);
  }
}

  RN调用

  var IOSHandler = require('react-native').NativeModules.IOSHandler;
        IOSHandler.goPage('').then((jsonObject) => {
   
            }
        });
View Code

 

posted @ 2019-03-07 16:19  云影风雨  阅读(955)  评论(0编辑  收藏  举报