1
votes

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?

3

3 Answers

2
votes

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

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

You can create a custom 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);
    }
}

and assign in to webView as :

webView.NavigationDelegate = new MyCustomWebViewDelegate();
0
votes

There is difference above iOS 12 to get cookie from WKWebview .You can have a try with getting cookie by invoking DecidePolicy method from WKNavigationDelegate .

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

The renderer code as follow :

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

Also can refer to this dicussion :How to Copy Cookie to WKWebview in iOS?