React-Native Hybrid App: Pop UIViewController embedding RCTRootView

I am integrating components into an existing iOS application written in Objective-C / Swift.

As the root view controller of my application, I use the UINavigationController .

In one of several view controllers of my application, I have a button that clicks the view controller in the navigation controller, which contains the following code:

 @objc class ReactNativeViewController: UIViewController { override func viewDidLoad() { let jsCodeLocation = NSURL(string: "http://localhost:8081/index.ios.bundle?platform=ios&dev=true") let contactsView = RCTRootView(bundleURL: jsCodeLocation, moduleName: "MainComponent", initialProperties: nil, launchOptions: nil) self.view.addSubview(contactsView) contactsView.frame = self.view.bounds; } } 

MainComponent returns a Navigator that manages several React-Native components:

 return ( <Navigator initialRoute={initialRoute} renderScene={(route, navigator) => { if (route.component) { return <route.component navigator={navigator} {...route.passProps} />; } }} navigationBar={ <Navigator.NavigationBar routeMapper={this.NavigationBarRouteMapper} style={styles.navBar} /> } /> ); 

This workflow is working fine. The only thing I need is a way to pop the ReactNativeViewController from my UINavigationController when the Back button was removed over the main React-Native component.

I tried the following but no luck:

  • Create your own module using one popLastViewController method, which displays the displayed UIViewController, which is displayed in the UINavigationController:

     @implementation RNNavigationControllerBridge RCT_EXPORT_MODULE() RCT_EXPORT_METHOD(popLastViewController) { UINavigationController *navigationController = (UINavigationController *)[[[[UIApplication sharedApplication] delegate] window] rootViewController]; [navigationController popViewControllerAnimated:NO]; } @end 
  • Call the above method when you click the back button:

     onPress={() => { if (index === 0) { NativeViewsManager.popLastViewController(); } else { navigator.pop(); } }} 

But that does not work.

Any suggestion?

+5
source share
1 answer

The problem is solved!

 RCT_EXPORT_METHOD(popLastViewController) { dispatch_async(dispatch_get_main_queue(), ^{ UINavigationController *navigationController = (UINavigationController *)[[[[UIApplication sharedApplication] delegate] window] rootViewController]; [navigationController popViewControllerAnimated:YES]; }); } 
+7
source

Source: https://habr.com/ru/post/1235692/


All Articles