Pir Shukarullah Shah
Pir Shukarullah Shah

Reputation: 4242

Go back to native app after launch of react native in existing project

I have successfully integrated react native in existing project. In my native app I have a screen with a button that launch react-native screen.

In Android I can go back to previous screen with help of BackAndroid.exitApp(). How can I achieve same functionality in iOS?

Upvotes: 0

Views: 1566

Answers (2)

Zain Sajjad
Zain Sajjad

Reputation: 21

This can be done by dismissing top presented view controller of your app. In order to get the top view controller in your RCTBridge implementation you can use this code.

- (UIViewController *)currentTopViewController
{
    UIViewController *topVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
    while (topVC.presentedViewController)
    {
        topVC = topVC.presentedViewController;
    }
    if ([topVC isKindOfClass:[UINavigationController class]]) {
        return [(UINavigationController *)topVC topViewController];
    }
    return topVC;
}

Then you can dismiss this view controller

[topViewController dismissViewControllerAnimated:YES completion:nil];

I developed a small npm package for achieving this. Find it here

Upvotes: 0

Peter Theill
Peter Theill

Reputation: 3220

When I do flow integrations between native and RN I simply add a hook back in my native app and call that from RN. So in your example I would do something like

onBackButtonPress() {
  if (Platform.OS === 'ios') {
    NativeModules.SomeController.backPressed()
  }
  else {
    BackAndroid.exitApp()
  }
}

and then define backPressed() in your controller so logic can be placed there

RCT_EXPORT_METHOD(backPressed) {
    // logic to go back e.g. pop view controller or dismiss modal
}

Upvotes: 1

Related Questions