Xamarin WKWebView 和 Cookie [英] Xamarin WKWebView and Cookies

查看:18
本文介绍了Xamarin WKWebView 和 Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Xamarin Forms 应用程序,它使用 cookie 来跟踪登录状态并同时使用 HTTPRequests 和 Webviews,因此两者都需要共享 cookie.使用 UIWebView 这个 cookie 是共享的,我没有任何额外的管理;对于 WKWebView,情况似乎并非如此.我一直在寻找有关如何使用 WKWebView 处理 cookie 的解释或如何在这两个对象之间手动检索和设置 cookie 的示例,但一直找不到.将 UIWebView 与 WKWebView 一起使用时,如何获得依赖的 cookie 行为?

I have a Xamarin Forms app that uses cookies to track login status and uses both HTTPRequests and Webviews, so both need to share cookies. With UIWebView this cookies were shared without any extra management on my part; with WKWebView this appears not to be the case. I have been searching for an explanation on how cookies are handled with WKWebView or an example of how to manually retrieve and set the cookies between these two objects, but have been unable to find any. How do I get the cookie behavior that I have relied on when using UIWebView with WKWebView?

推荐答案

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

When I tried to implement a WKNamvigationDelegate the WebView OnLoadFinished was not called, so my loading indicator remained after loading was complete. What ended up working for me is in my iOS CustomWebViewRenderer's constructor I call this function to clear out any existing cookies and copy any cookies from the HTTP Shared Storage into the webview:

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 复制到共享存储中.

To get the cookies from the webview I have existing in shared code a CustomWebView that uses OnShouldLoad to detect the indication of a successful login, then call platform specific code. This was created to handle Android cookies, but will now work for iOS as well. The iOS implementation clears out any existing HTTP shared storage cookies and copies the cookies from the webview into the Shared Storage.

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;
}

这篇关于Xamarin WKWebView 和 Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆