Reputation: 4242
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
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
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