Is there an elegant way to access the camera from

2019-06-06 07:29发布

问题:

I am currently developing an hybrid app with Cordova. The project is mainly a webview for an external website (implemented with the Cordova inappbrowser plugin). On this site I want the user to be able to take a picture with their device camera and use it on the website. On Android I intercept the "loadstart" event and launch the camera plugin for a specific url, which works just as I want it to. On iOS however, the camera does not launch and I get a warning
Warning: Attempt to present <CDVCameraPicker: 0xXXXXXXXXX> on <MainViewController: 0xXXX> whose view is not in the window hierarchy!
in XCode.
After some research I found out, that this is expected behaviour for the inappbrowser. Closing the inappbrowser to launch the camera and open the inappbrowser again is no option for me.

Question 1: Is it possible to show the camera on top of the inappbrowser?

My workaroud so far is to use <input type="file" accept="image/*" capture="camera">, which works (kind of). It opens a dialog where the user can choose between "take picture" and "choose file", if he chooses "take picture" the camera opens and I can get the picture to the website.

Question 2: Can I bypass this choose dialog and go directly to the camera when the button is clicked?

I know that both modifications have to take place in native iOS code (which I have never used).
Any help will be appreciated and THANK YOU for your time.

Related question:

  • Cordova IOS camera plugin not working when trying to open from inappbrowser


Update:

The call which seems to break the camera is:

__weak CDVCamera* weakSelf = self;

...

[weakSelf.viewController presentViewController:cameraPicker animated:YES completion:^{
    weakSelf.hasPendingOperation = NO;
}];

Question: How can I present the camera on top of the inappbrowser plugin?

I tried to get the rootViewController but it did not work:

  • [[[[UIApplication sharedApplication] delegate] window] rootViewController]