I have found that, when compiling for iOS 8 (and running in iOS 8), a UIWebView
cannot show the camera/image picker if the UIWebView
is in a view controller presented modally. It works without problem in view controllers directly “hanging” from the window rootViewController
or view controllers pushed from it.
The test application can be found at https://dl.dropboxusercontent.com/u/6214425/TestModalWebCamera.zip but I will describe it below.
My test application (built with storyboards, but the real application doesn’t use them) has two view controllers (unoriginally named ViewController
and ViewController2
). ViewController
is contained in a UINavigationController
which is the root view controller. ViewController
contains a UIWebView
(works OK), a button that “shows” (“pushes”) ViewController2
, and a UIBarButtonItem
which modally presents ViewController2
. ViewController2
has another UIWebView
which works when “pushed” but not when “presented”.
Both ViewController
and ViewController2
are loaded with:
- (void)viewDidLoad {
[super viewDidLoad];
[self.webView loadHTMLString:@"<input type=\"file\" accept=\"image/*;capture=camera\">" baseURL:nil];
}
When trying to use the modal UIWebView
Xcode prints the following in the console and dismisses the app modal:
Warning: Attempt to present <UIImagePickerController: 0x150ab800> on <ViewController2: 0x14623580> whose view is not in the window hierarchy!
My current theory is that the changes in UIActionSheet
to UIAlertController
might have produced this situation, but it’s quite hard to prove. I will open a Radar with Apple, just in case.
Has someone found the same situation and some workaround?
What I did what every time the view controller is change, convert the destination view in root.
from first view controller:
this is how I pass the root to the other, and when comeback to the first one I made this.
from web view controller:
and everything is fine, I can show the modal for select photo from library, take photo and everything else.
I don't know if that is a good practice but works fine in emulator and device.
Hope it helps.
I found that in iOS 8.0.2 iPad does not seem to have that bug but iPhone still does.
However, overriding following in the view controller containing the uiwebview
And checking that there is a presentedViewController seems to work.
But need to check side effects
For me I tend to have a custom UINavigationController so that multiple views can share the same logic. So for my workaround (in Swift) here is what I put in my custom NavigationController.
This means that you can have loads of view controllers with webviews and they'll all work with the file upload element.
My solution is to create custom View Controller with custom modal presentation based on controllers child-parent hierarchy. Animation will be the same so the user will not notice the difference.
My suggestions for animation:
It will look exactly like default modal presentation animation in iOS7/8.
I was having a similar issue, and I have discovered that the UIWebView elements in IOS do not support the html element:
I am not sure why Apple chose to not support this IMPORTANT html element, but I am sure they have their reasons. (Even though this element works perfectly on Safari on IOS.)
In many cases, when the user clicks this kind of button in a UIWebView, it will let them take/ choose a photo. HOWEVER, the UIWebView in IOS does not have the capability to attach files like this into the POST data when the form is submitted.
The Solution: To accomplish the same task you can create a similar form in InterfaceBuilder with a button that triggers the UIImagePickerController. Then, you create you an HTTP POST request with all of the form data and the image. It isn't as hard as it sounds, check out the link below for some sample code that gets the job done: ios Upload Image and Text using HTTP POST
I have the same issue on iOS 9. Try to add ivar '_flag' and then override this methods in view controller with UIWebView
This works for me fine