How to dismiss view controller from react native?

Viewed 1942

Integration with exsiting apps doc shows how to present view controller whose content is react native .

RCTRootView *rootView =
  [[RCTRootView alloc] initWithBundleURL: jsCodeLocation
                              moduleName: @"RNHighScores"
                       initialProperties:
                         @{
                           @"scores" : @[
                             @{
                               @"name" : @"Alex",
                               @"value": @"42"
                              },
                             @{
                               @"name" : @"Joel",
                               @"value": @"10"
                             }
                           ]
                         }
                           launchOptions: nil];
UIViewController *vc = [[UIViewController alloc] init];
vc.view = rootView;
[self presentViewController:vc animated:YES completion:nil];

What it doesn't tell you is how to dismiss the viewcontroller since we are in react native (javascript world) not objc/swift world anymore..

How can I call native dimiss... function on the viewcontroller?

1 Answers

Check this repo out: tejasd/ios-playground

Basic idea:

1.Import a native iOS class to React Native page, this iOS class does one thing: post a notification

let DismissViewControllerManager = NativeModules.DismissViewControllerManager;

2.Post a notification in React Native page

onButtonPress: () => {
                        DismissViewControllerManager.goBack();
                    }

3.Handle that notification in native iOS page and call dismiss

// add observer in viewDidLoad
NotificationCenter.default.addObserver(self,
                                               selector: #selector(goBack(_:)),
                                               name: NSNotification.Name("dismissViewController"),
                                               object: nil)

// remove observer in deinit (iOS 8 or earlier)
NotificationCenter.default.removeObserver(self)

// handle dismiss notification
func goBack(_ sender: Any?) -> Void {
        self.dismiss(animated: true, completion: nil)
    }
Related