WKWebview third party cookie permission

ErdoganErdogan Member ✭✭✭

How to allow third-party cookies of the Custom WKWebview in ios 13.1 or .2?

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    We could use NSHTTPCookieStorage to change the policy on UIWebView but Apple has deprecated this control and encourage us to use WKWebView.
    And there's no such an api to always accept the cookies policy for WKWebView. We can only add them manually in its navigation delegate like:
    https://stackoverflow.com/questions/53766434/set-wkwebview-cookie-accept-policy-in-ios-12
    However, the author said it lost effect after iOS 12. You could have a try on iOS 13.3. If it still doesn't work I think we can't make it on iOS 13.

  • ErdoganErdogan Member ✭✭✭

    Thanks @LandLu

    Yes, Actually, I use the NSHTTPCookieStorage but I guess it's a bug for between iOS 13.0 - 13.2.

    There are some solutions with external plug-ins;
    https://bugs.webkit.org/show_bug.cgi?id=200857

  • LandLuLandLu Member, Xamarin Team Xamurai

    WKWebView has its own cookie storage:

    webView.Configuration.WebsiteDataStore.HttpCookieStore
    

    So it won't use shared NSHTTPCookieStorage. I saw some workarounds are to embed the cookies in the first request. However, further cookies from website will also be lost.
    Have you tried to add it in the delegate manually as what I posted above?

    but I guess it's a bug for between iOS 13.0 - 13.2.

    It's hard to say. Apple has its right to allow it or forbidden it.

  • ErdoganErdogan Member ✭✭✭

    Yes Landlu, I use the CookieStorage for all of the versions. Apple fixed it in 13.3 but I need to find one way to do it for 13.0 - 13.2 but I guess, there is no luck to do it.

    This is my delegate;

    public class NavigationDelegate : WKNavigationDelegate
        {
            NSMutableArray multiCookieArr = new NSMutableArray();
    
            [Foundation.Export("webView:decidePolicyForNavigationResponse:decisionHandler:")]
            public override void DecidePolicy(WKWebView webView, WKNavigationResponse navigationResponse, [BlockProxy(typeof(Action))]Action<WKNavigationResponsePolicy> decisionHandler)
            {
                if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
                {
                    Uri url = navigationResponse.Response.Url;
                    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);
                            }
                        }
                    });
                }
                else
                {
                    NSHttpUrlResponse response = navigationResponse.Response as NSHttpUrlResponse;
                    NSHttpCookie[] cookiesAll = NSHttpCookie.CookiesWithResponseHeaderFields(response.AllHeaderFields, response.Url);
                    foreach (NSHttpCookie cookie in cookiesAll)
                    {
                        NSArray cookieArr = NSArray.FromObjects(cookie.Name, cookie.Value, cookie.Domain, cookie.Path);
                        multiCookieArr.Add(cookieArr);
                    }
                    Console.WriteLine("cookie is :" + cookiesAll);
                }
    
                decisionHandler(WKNavigationResponsePolicy.Allow);
            }
        }
    
  • LandLuLandLu Member, Xamarin Team Xamurai

    We could do nothing for the previous version of iOS.
    It seems Apple allowed us to use cookies again on iOS 13.3. It's good news. The best suggestion is to force the user consuming the latter version of the 13 series.

Sign In or Register to comment.