-
Notifications
You must be signed in to change notification settings - Fork 24.8k
Description
I had this requirement of adding a new screen to an old iOS app made with Objective-C. The next thought was to make a react native reusable component and open it from my iOS app.
I used the reference from this page: https://facebook.github.io/react-native/docs/integration-with-existing-apps, to open my react native component.
The point where I am stuck right now is, how to go back to my native app? There's is nothing in the tutorial for that.
Here is my code:
1.) How to present a react native component from iOS app:
NSURL *jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.bundle?platform=ios"];
RCTRootView *rootView =
[[RCTRootView alloc] initWithBundleURL: jsCodeLocation
moduleName: @"MyListView"
initialProperties:nil
launchOptions: nil];
UIViewController *vc = [[UIViewController alloc] init];
vc.view = rootView;
[self presentViewController:vc animated:YES completion:nil];
2.) How to expose an Objective-C class to React Native. Write the below code in the .m file:
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(dismiss: (NSString *) name){
NSLog(@"dismiss");
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil];
});
}
3.) Now how to call dismiss method from React-Native to Objective C file:
var ViewController = NativeModules.ViewController;
backTap = () => {
ViewController.dismiss('Blah');
}
Now NativeModules.ViewController will always create a new instance of ViewController. How can I get the actual instance of ViewController, so I can dismiss the react-native component?