Xamarin WKWebView 和 Cookie

Xamarin WKWebView and Cookies

我有一个 Xamarin Forms 应用,它使用 cookie 来跟踪登录状态并同时使用 HTTPRequests 和 Webview,因此两者都需要共享 cookie。使用 UIWebView 可以共享这些 cookie,而无需我进行任何额外管理;对于 WKWebView,情况似乎并非如此。我一直在寻找关于如何使用 WKWebView 处理 cookie 的解释或如何在这两个对象之间手动检索和设置 cookie 的示例,但一直找不到任何内容。 UIWebView 和 WKWebView 一起使用时,如何获取我所依赖的 cookie 行为?

您可以创建自定义 WKNavigationDelegate。

public class MyCustomWebViewDelegate : WKNavigationDelegate
{
    public override void DecidePolicy(WKWebView webView, WKNavigationAction navigationAction, Action<WKNavigationActionPolicy> decisionHandler)
    {
        var jCookies = Appl.FlurlClient.Cookies.Values;
        NSHttpCookie[] nsHttpCookies = jCookies.Where(c => c != null).Select(c => new NSHttpCookie(c)).ToArray();

        foreach (var c in nsHttpCookies)
        {
            webView.Configuration.WebsiteDataStore.HttpCookieStore.SetCookie(c);
        }
        decisionHandler(WKNavigationActionPolicy.Allow);
    }
}

并分配给 webView 为:

webView.NavigationDelegate = new MyCustomWebViewDelegate();

iOS 12 从 WKWebview 获取 cookie 有区别。您可以尝试通过从 WKNavigationDelegate[= 调用 DecidePolicy 方法获取 cookie 22=]。

public class NavigationDelegate : WKNavigationDelegate
{
    NSMutableArray multiCookieArr = new NSMutableArray();

public override void DecidePolicy(WKWebView webView, WKNavigationResponse navigationResponse, [BlockProxy(typeof(Action))]Action<WKNavigationResponsePolicy> decisionHandler)
{

    if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
    {
        WKHttpCookieStore wKHttpCookieStore = webView.Configuration.WebsiteDataStore.HttpCookieStore;
        Console.WriteLine("wKHttpCookieStore is :" + wKHttpCookieStore.GetDebugDescription());
        wKHttpCookieStore.GetAllCookies(cookies => {
            if(cookies.Length > 0)
            {
                foreach (NSHttpCookie cookie in cookies)
                {
                    //NSHttpCookieStorage.SharedStorage.SetCookie(cookie);
                    Console.WriteLine("cookie is :" + cookie);
                }

            }
        });
    }
    else
    {
        NSHttpUrlResponse response = navigationResponse.Response as NSHttpUrlResponse;
        NSHttpCookie[] cookiesAll = NSHttpCookie.CookiesWithResponseHeaderFields(response.AllHeaderFields, response.Url);
        foreach (NSHttpCookie cookie in cookiesAll)
        {
            Console.WriteLine("Here is the cookie inside wkwebview is :" + cookie);
            NSArray cookieArr = NSArray.FromObjects(cookie.Name, cookie.Value, cookie.Domain, cookie.Path);
            multiCookieArr.Add(cookieArr);
        }
        Console.WriteLine("cookie is :" + cookiesAll);
    }

    decisionHandler(WKNavigationResponsePolicy.Allow);

    //base.DecidePolicy(webView, navigationResponse, decisionHandler);
}

渲染器代码如下:

public class HybridWebViewRenderer : WkWebViewRenderer
{

    public HybridWebViewRenderer() : this(new WKWebViewConfiguration())
    {
    }

    public HybridWebViewRenderer(WKWebViewConfiguration config) : base(config)
    {   
    }

    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null)
        {
          //...
        }

        if (e.NewElement != null)
        {
            this.NavigationDelegate = new NavigationDelegat();
        }
    }
}

也可以参考这个讨论:

当我尝试实现 WKNamvigationDelegate 时,未调用 WebView OnLoadFinished,因此加载完成后我的加载指示器仍然存在。最终对我有用的是在我的 iOS CustomWebViewRenderer 的构造函数中我调用此函数来清除任何现有的 cookie 并将任何 cookie 从 HTTP 共享存储复制到 web 视图中:

protected async void SetCookies()
{
    var dataStore = WKWebsiteDataStore.DefaultDataStore;
    var cookies = NSHttpCookieStorage.SharedStorage.Cookies;
    var oldcookies = await dataStore.HttpCookieStore.GetAllCookiesAsync();
    foreach (var cookie in oldcookies)
    {
        await dataStore.HttpCookieStore.DeleteCookieAsync(cookie);
    }
    foreach (var cookie in cookies)
    {
        await dataStore.HttpCookieStore.SetCookieAsync(cookie);
    }
} 

为了从 webview 中获取 cookie,我在共享代码中存在一个 CustomWebView,它使用 OnShouldLoad 来检测成功登录的指示,然后调用特定于平台的代码。这是为了处理 Android cookie 而创建的,但现在也适用于 iOS。 iOS 实现清除了任何现有的 HTTP 共享存储 cookie,并将 cookie 从 webview 复制到共享存储中。

public async Task GetCookiesFromWebview()
{
    var dataStore = WKWebsiteDataStore.DefaultDataStore;
    var cookies = await dataStore.HttpCookieStore.GetAllCookiesAsync();
    var oldcookies = NSHttpCookieStorage.SharedStorage.Cookies;
    foreach (var cookie in oldcookies)
    {
        NSHttpCookieStorage.SharedStorage.DeleteCookie(cookie);
    }
    foreach (var cookie in cookies)
    {
        NSHttpCookieStorage.SharedStorage.SetCookie(cookie);
    }
    return;
}