Modal view closes when selecting an image in UIWebView iOS
Asked Answered
P

3

7

I'm currently building an app with a modal view popping up that contains a WkWebView. When I want to upload an image within this modal view and the Photo Selection appears, the modal view just dismisses back to the view controller that fired it up.

How can I prevent that?

import UIKit

class PostWindow : UIViewController {

@IBAction func close(sender: AnyObject) {
    dismissViewControllerAnimated(true, completion: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()
    // do stuff here
    let myWebView:UIWebView = UIWebView(frame: CGRectMake(0, 70, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
    myWebView.loadRequest(NSURLRequest(URL: NSURL(string: "https://m.facebook.com/")!))
    self.view.addSubview(myWebView)

    self.title = "News Feed"

    UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.Default, animated: true)
    UIApplication.sharedApplication().statusBarHidden = false

    /*let addButton: UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search,
    target: self,
    action: #selector(self.openSearch(_:)))
    self.navigationItem.setRightBarButtonItems([addButton], animated: true)*/
    self.navigationController?.navigationBar.tintColor = UIColor.blackColor()
}

override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return UIStatusBarStyle.LightContent
}

}

Thanks!

Plauen answered 22/5, 2016 at 22:30 Comment(0)
A
13

I encountered the same issue. I found that the file upload action sheet tries to dismiss itself twice upon selecting an option, which results in the modal being dismissed as well.

A solution is to subclass the UINavigationController containing the webview and override dismissViewControllerAnimated to ignore it unless it actually has a presentedViewController.

Like so:

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

If you're not using a navigation controller, just override this method in the webview instead.

Agranulocytosis answered 7/7, 2016 at 14:51 Comment(0)
Q
3

For those who struggle with no fix by using func dismiss() you can use this trick:

extension UIImagePickerController {
  open override func viewDidLoad() {
    super.viewDidLoad()
    self.modalPresentationStyle = .overFullScreen // this will turn off dismiss of webView when imagePicker presents
  }
}

Qualification answered 9/9, 2021 at 9:53 Comment(1)
Genius. This works. I tried all kind of things with Adaptive delegates. I am writing SwiftUI code and it works well. You should have more upvotes!! Thanks!!Enrich
H
0

It also works for non NavigationController / ViewController only in this way:

override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil){
    if(self.presentedViewController != nil) 
    {
        super.dismiss(animated: flag, completion: completion)
    }
}
Hodosh answered 11/12, 2017 at 19:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.