4

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?

rmaddy
  • 298,130
  • 40
  • 468
  • 517
PashaN
  • 292
  • 2
  • 16

1 Answers1

-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;
    }
StefanoM5
  • 1,237
  • 1
  • 22
  • 32