WebView select file from photo library

12,012

Solution 1

I was dealing with this issue for a while when I was working more often with ViewControllers.

Adding this following code to the ViewController with the WebView does solve the problem in the iOS Simulator.

override func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) 
{
    if self.presentedViewController != nil {
        super.dismissViewControllerAnimated(flag, completion: completion)
    }
}

However, I still had the problem when running on an actual device. To fix it, I added that code in the MainViewController as well.

Hope this solves it and happy coding.

Solution 2

I tried using Sasha Nicolas' hack but it won't work right now with the new Swift Version. I'm using Swift 4 right now. Xcode gave me some hints and this is what worked fine.

override func dismiss(animated flag: Bool, completion: (() -> Void)?) {
    if self.presentedViewController != nil {
        super.dismiss(animated: flag, completion: completion)
    }
}

I placed it at the very end of my view controller code, just before the } of my main class.

Don't forget to add permissions for camera and photos in info.plist and this code at the beginning of you class in you view controller.

class ViewControllerLogado: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate {}

Solution 3

A fast workaround for this is to add the following code to the ViewController with the WebView:

override func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) {
    if self.presentedViewController != nil {
        super.dismissViewControllerAnimated(flag, completion: completion)
    }
}

This post is related to your question: iOS 8 SDK: modal UIWebView and camera/image picker

Share:
12,012
Mr Riksson
Author by

Mr Riksson

Updated on June 05, 2022

Comments

  • Mr Riksson
    Mr Riksson almost 2 years

    I'm working on a project where I'm using 2 views, MainView and SecondView. MainView is more or less a start pages and then I use the segue function to go from MainView to SecondView.

    In SecondView I have WebView and on this website I have a file uploader. So the user should be able to upload images from his or her photo library to the site.

    The website is accessed with this code:

    if let url = NSURL(string: "https://example.com/uploader") {
            webView.loadRequest(NSURLRequest(URL: url))
        }
    

    The problem is that when I try to upload an image it only segues back to my MainView

    enter image description here

    So when I press Photo Library it segues back to MainView.

    However I recreated the WebView version of the app in a new project with only one view and then everything works like a charm as shown below

    enter image description here

    So basically my question is, how do I prevent the app from segueing back when I select Photo Library?

    Link to Download the project and see for yourself.