The modal view closes when you select an image in WkWebView iOS

I am currently creating a trendy viewing application containing WkWebView. When I want to upload an image in this modal view, and β€œSelect Photos” appears, the modal view simply goes back to the view controller that activated it.

How can I prevent this?

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!

+6
source share
2 answers

I ran into the same problem. I found that the file upload action file is trying to double away from itself when choosing an option, which also leads to the fact that the modal object is also rejected.

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

Same:

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

If you are not using a navigation controller, simply override this method in webview.

+9
source

It also works for non NavigationController / ViewController only as follows:

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

All Articles