I want to display a native view controller (ABPersonViewController) within a react native application.
I can present the view but I cannot push the view. This code fragment works and presents the view but there is no back button to my app:
let personViewController = ABPersonViewController()
let rootViewController:UIViewController?
= UIApplication.shared.delegate?.window??.rootViewController!
personViewController.displayedPerson = record!
// this works
rootViewController!.present(personViewController, animated: true)
If I try to push the view rather than present the view, nothing happens because navigationController is nil:
if (rootViewController!.navigationController != nil) {
rootViewController!.navigationController!.pushViewController(personViewController, animated: true)
}
How can I push a native view within react native iOS?
In React native android, view contacts can easily be done using this code in a custom module:
final Activity activity = getCurrentActivity();
Intent contactIntent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, id);
contactIntent.setData(uri);
activity.startActivity(contactIntent);
I wish it was that easy in react native iOS!
I tried @Artal's solution for iOS and it works up to a point.
I allocated a UINavigationController that used the existing RN UIViewController as its root and set it as the rootViewController of the window. Here is the code:
UIViewController *rootViewController = [UIViewController new];
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:rootViewController];
navigationController.navigationBarHidden = YES;
rootViewController.view = rootView;
self.window.rootViewController = navigationController;
In my swift code I then cast the rootViewController to UINavigationController, set isNavigationBarHidden to false and pushed the personViewController
let navigationController:UINavigationController = rootViewController as! UINavigationController;
DispatchQueue.main.async {
navigationController.isNavigationBarHidden = false;
navigationController.pushViewController(personViewController, animated: true)
}
The problem came when I overrode the UINavigationController ShouldPopOnBackButton in order to set navigationBarHidden to true when I returned to RN. I followed the approach in https://stackoverflow.com/a/19132881/826435
@implementation UINavigationController (ShouldPopOnBackButton)
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item {
if([self.viewControllers count] < [navigationBar.items count]) {
return YES;
}
dispatch_async(dispatch_get_main_queue(), ^{
self.navigationBarHidden = YES;
[self popViewControllerAnimated:YES];
});
return NO;
}
@end
AftershouldPopItem is called, the RN view is restored but the RN keyboard is then subsequently disabled for all input fields when running on the simulator; it works fine on a device though.
Any ideas why the RN keyboard on the simulator is disabled?
James