React Native 调用 iOS ViewController

Rai*_*iao 5 ios react-native

在 RN 中如何调用原生 iOS viewController 的代码?

就像使用 RN 推送到本机 iOS viewController,然后让本机代码完成这项工作。

Rah*_*hul 3

您需要使用导出方法。

AppDelegate.h

#import <UIKit/UIKit.h>
#import "RCTBridgeModule.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate,RCTBridgeModule>

@property (nonatomic, strong) UIWindow *window;

@end
Run Code Online (Sandbox Code Playgroud)

AppDelegate.m

@implementation AppDelegate
RCT_EXPORT_MODULE()

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //NO Change Here
}



RCT_EXPORT_METHOD(pushVC:(NSString *)vcName){

      Class ctrlClass = NSClassFromString(vcName);
      UIViewController *newVc = [[ctrlClass alloc] initWithNibName: vcName bundle: nil];
      [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:newVc animated:YES completion:nil];
    }
Run Code Online (Sandbox Code Playgroud)