如何将一个 WKWebView 中的所有内容复制到新的 WKWebView

Pas*_*haN 7 ios swift wkwebview wkwebviewconfiguration

我正在尝试将所有加载的数据从一个复制WKWebView到一个新数据,以便在呈现WKWebView新内容时加载所有内容。webView有什么建议如何做到这一点或者最好的方法是什么?

Ste*_*oM5 -1

我在 Xamarin 表单中找到了一个解决方案,我希望这可以帮助您,请理解这个想法而不是代码

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;
}
Run Code Online (Sandbox Code Playgroud)

当你实例化一个新的 webview 时,你必须以这种方式复制配置:

private async Task CopyConfiguration(String url, MgFormsWebView mainWebview)
    {
        var config = await mainWebview.GetConfigurationAsync();
        await webview.SetConfigurationAsync(config);
        webview.Source = url;
    }
Run Code Online (Sandbox Code Playgroud)