Xamarin WKWebView和Cookies [英] Xamarin WKWebView and Cookies

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

问题描述

我有一个Xamarin Forms应用程序,该应用程序使用cookie来跟踪登录状态,并同时使用HTTPRequest和Webview,因此两者都需要共享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共享存储复制到webview中:

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,该CustomWebView使用OnShouldLoad检测成功登录的指示,然后调用特定于平台的代码.它是为处理Android cookie而创建的,但现在也适用于iOS.iOS实施会清除所有现有的HTTP共享存储Cookie,并将这些Cookie从Web视图复制到共享存储中.

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和Cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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