3

I have used Handling new window inside webview. I have used solution 2 from the mentioned link, Which was adding the new window to the subview.

This solution provided in this link works pretty well. But the problem I faced here is when I open multiple windows in webview I'm not able to close the parent window through which it was opened. The opening and closing of window is controlled by simple code written on remote server simply consisting of window.open("some_valid_url") and window.close() to open and close window respectively.

I'm giving webview to let's say www.someValidUrl.com which has code to open the window and close the window.

For example: Main Page --> Then Main Page opens window A--> Then window A Opens window B, Now if I close window B then try to close window A, window A won't be closed.

As a result I'm not able to return to Main page.

I'm adding the code which is used to open and close windows respectively :

class ViewController: UIViewController, UIWebViewDelegate, CLLocationManagerDelegate,WKUIDelegate,WKNavigationDelegate
{
    @IBOutlet weak var webView: WKWebView!
    var popupWebView: WKWebView?
    ...
    func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
            popupWebView = WKWebView(frame: view.bounds, configuration: configuration)
            popupWebView!.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            popupWebView!.navigationDelegate = self
            popupWebView!.uiDelegate = self
            view.addSubview(popupWebView!)
            return popupWebView!
        }
    func webViewDidClose(_ webView: WKWebView) {
        if webView == popupWebView {
            popupWebView?.removeFromSuperview()
            popupWebView = nil
        }
    }
    ...
}

I have shared the only code which was used for windows opening and closing. I want to allow opening and closing of multiple windows in my application. Any help would be greatly appreciated.

Please ask if you need any further details on the question.

Jennis Vaishnav
  • 330
  • 6
  • 29

1 Answers1

2

Just after doing a bit of search and trial and error I found answer to my own question, By changing the code inside webViewDidClose as follows:

    func webViewDidClose(_ webView: WKWebView) {
        webView.removeFromSuperview()
        popupWebView = nil
    }

Hope this helps someone else who faces the same issue.

Jennis Vaishnav
  • 330
  • 6
  • 29