WKCookieWebView icon indicating copy to clipboard operation
WKCookieWebView copied to clipboard

Preload from `createWebViewWith`

Open alekop opened this issue 5 years ago • 1 comments

Nice work!

Any suggestions on how to preload when creating a Web view dynamically, e.g. in response to window.open()? I'm loading third-party content, so I don't know if/when the content wants to open another page.

public func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration,
                    for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {

    // Added an initializer to accept an existing configuration
    let newWebView = WKCookieWebView(configuration: configuration)

    if let url = navigationAction.request.url {
        // Doesn't work, probably because we have to return the WebView synchronously,
        // before we have a chance to preload. Loading later doesn't work.
        WKCookieWebView.preloadWithDomainForCookieSync(urlString: url.absoluteString) {
            newWebView.load(navigationAction.request)
        }
    }

    // Add webview to tab list
    myDelegate?.didOpenNewWebView(for: url)

    return newWebView
}

alekop avatar Sep 23 '19 22:09 alekop

Hi, I think the idea you suggested was a good idea. I'll try to figure out a way to apply it. Or you can do the PR yourself. ^^

Kofktu avatar Sep 25 '19 02:09 Kofktu