Mr Riksson Mr Riksson - 1 month ago 15
Swift Question

WebView select file from photo library

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.

Answer

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

Adding this 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, The fix for that was to add that code in the MainViewController as well.

Hope this solves it and happy coding.

Comments