How to move in AND OUT of Native Module in React Native?

Viewed 86

My question has to do with native modules in React and how to switch between Js views and a Swift written native modules. The code I have is pretty boilerplate from various examples.

The goToNativeView function below swaps out the rootViewController to enter the Swift code (through the Main storyboard file). How would I instrument a function call from Swift to return back to my Javascript application?

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
#ifdef FB_SONARKIT_ENABLED
  InitializeFlipper(application);
#endif

  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"RNImageCollectorV2"
                                            initialProperties:nil];

  if (@available(iOS 13.0, *)) {
      rootView.backgroundColor = [UIColor systemBackgroundColor];
  } else {
      rootView.backgroundColor = [UIColor whiteColor];
  }

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [UIViewController new];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  return YES;
}

// this method will be called from the RCTBridge
- (void) goToNativeView {
    NSLog(@"RN binding - Native View - CameraViewController.swift - Load From main storyboard");
    UIViewController *vc = [UIStoryboard storyboardWithName:@"Main" bundle:nil].instantiateInitialViewController;
    self.window.rootViewController = vc;
}

In my React Component, I have a simple function that enables the native module. I'm assuming I need a callback or listener for the exit call

App.tsx

class App extends Component {

  constructor(props) {
    super(props)
  }

  _changeView = () => {
    // this.done = true;
    // this.render();
    NativeModules.ChangeViewBridge.changeToNativeView();
  };

  render() {
    return (
       <TouchableHighlight onPress={() => this._changeView()}>
                <Text color="#336699">Press to Change to Native View</Text>
              </TouchableHighlight>
  )}
0 Answers
Related