2

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.

Mr Riksson
  • 560
  • 3
  • 10
  • 29
  • Can you post the code being used to select the photo from the photo library? – Caleb Bach Nov 12 '15 at 12:43
  • This is completely done from the website. Using Wordpress and Contact Form 7 plugin with the file upload feature. – Mr Riksson Nov 12 '15 at 12:56
  • I think if you dint 'self.navigationController?.pushViewController' and used 'present' some how if using pushviewcontroller. it will go back to your rootviewcontroller. hence i make a Present view. and make custom Title for the webview – Muhammad Asyraf Apr 10 '18 at 05:00
  • Is there a way to handle selection of file type within the code? Or manipulate what file type should get selected – Anuranjan Bose Jul 01 '20 at 09:08

5 Answers5

3

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.

Sasha Nicolas
  • 159
  • 1
  • 9
Kevin
  • 1,435
  • 4
  • 19
  • 27
  • but how am i going to used " self.dismiss(animated: true, completion: nil)" seem like i cant use it at all this view controller – Muhammad Asyraf Apr 10 '18 at 11:14
3

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 {}
Kukic Vladimir
  • 1,010
  • 4
  • 15
  • 22
  • after uploading file is your web view reload again? I've checked with real device and simulator. Simulator working fine but In real device it reloading. – Bhavin Chauhan Apr 03 '19 at 14:52
2

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

Community
  • 1
  • 1
alephao
  • 1,234
  • 13
  • 20
0

For Swift 3.0

override func dismissViewControllerAnimated(flag: Bool, completion: (() -> Void)?) 
{
    if self.presentedViewController != nil 
    {
       super.dismiss(animated: flag, completion: completion)
    }
}
Siddharth Chauhan
  • 1,289
  • 8
  • 15
0

I added this

NSPhotoLibraryUsageDescription to the info.plist file

and the description of how the file to be used. it works for me with the Xcode 8.2.1