简体   繁体   中英

React Native call iOS ViewController

How to call native iOS viewController' code when in RN?

Like use RN to push to a native iOS viewController and then let the native code do the job.

You need to use Export Method .

AppDelegate.h

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

@interface AppDelegate : UIResponder <UIApplicationDelegate,RCTBridgeModule>

@property (nonatomic, strong) UIWindow *window;

@end

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];
    }

You can call above response in javascript like this:

import { NativeModules } from 'react-native'
NativeModules.AppDelegate.pushVC('viewControllerNameHere')

But I highly recommend to make with external file to bridge like documentation explain here:https://facebook.github.io/react-native/docs/native-modules-ios

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM