How do i copy everything from one WKWebView to a new WKWebView
Asked Answered
N

1

7

I'm trying to copy all loaded data from one WKWebView to a new WKWebView so all content would be loaded when new webView is presented. Any suggestion how to do this or what would be the best approach to this?

Nodose answered 7/12, 2017 at 16:55 Comment(2)
Hey, @Nodose did you manage to solve this? I'm stuck with the same problem as well.Embargo
@Embargo No, i just used one web view.Nodose
P
-1

I found a solution in Xamarin forms, i hope this can help you, please to understand the idea and not the code

async Task<List<object>> OnGetConfigurationRequestAsync()
{
    await Task.Delay(1);
    List<object> list = new List<object>();
    list.Add(_configuration);
    list.Add(_contentController);
    return list;
}

async Task<object> OnSetConfigurationRequestAsync(List<object> list)
{
    await Task.Delay(1);
    if (list == null) return null;
    WKWebViewConfiguration wkConfig = null;
    WKUserContentController wkUser = null;

    foreach (var item in list)
    {
        if (item is WKWebViewConfiguration)
            wkConfig = (WKWebViewConfiguration)item;
        else if (item is WKUserContentController)
            wkUser = (WKUserContentController)item;
    }

    if(wkConfig!=null)
    {
        _configuration = wkConfig;
        _contentController = wkUser;
    }

    var wkWebView = new WKWebView(Frame, _configuration)
    {
        Opaque = false,
        UIDelegate = this,
        NavigationDelegate = _navigationDelegate,
    };

    SetNativeControl(wkWebView);
    OnControlChanged?.Invoke(this, wkWebView);

    return null;
}

When you instantiate a new webview, you have to copy configuration in this way:

private async Task CopyConfiguration(String url, MgFormsWebView mainWebview)
    {
        var config = await mainWebview.GetConfigurationAsync();
        await webview.SetConfigurationAsync(config);
        webview.Source = url;
    }
Plovdiv answered 15/4, 2019 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.