【问题标题】:Why window.close is not working for multiple windows为什么 window.close 不适用于多个窗口
【发布时间】:2019-09-23 06:29:07
【问题描述】:

我用过Handling new window inside webview。我使用了上述链接中的解决方案 2,即将新窗口添加到 subview

此链接中提供的此解决方案效果很好。但是我在这里遇到的问题是,当我在webview 中打开多个窗口时,我无法关闭打开它的父窗口。窗口的打开和关闭由远程服务器上编写的简单代码控制,该代码仅由window.open("some_valid_url")window.close()组成,分别用于打开和关闭窗口。

我给 webview 比方说 www.someValidUrl.com 有代码来打开窗口和关闭窗口。

例如: 主页 --> 然后主页打开窗口 A--> 然后窗口 A 打开窗口 B,现在如果我关闭窗口 B 然后尝试关闭窗口 A,则窗口 A 不会关闭。

因此我无法返回主页。

我正在添加分别用于打开关闭窗口的代码:

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
        }
    }
    ...
}

我已经分享了用于打开和关闭窗口的唯一代码。我想允许在我的应用程序中打开和关闭多个窗口。任何帮助将不胜感激。

请询问您是否需要有关该问题的更多详细信息。

【问题讨论】:

    标签: javascript swift wkwebview wkwebviewconfiguration


    【解决方案1】:

    经过一番搜索和反复试验后,我找到了自己问题的答案,通过将webViewDidClose 中的代码更改如下:

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

    希望这对面临同样问题的其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-16
      • 2015-02-18
      • 1970-01-01
      • 1970-01-01
      • 2019-09-23
      • 1970-01-01
      相关资源
      最近更新 更多