Xamarin.Forms Ios Webviewrenderer sharing Cookies

Asked

Viewed 124 times

0

I am using a Webviewrenderer to configure the cookie policy and also to share cookies from an Httpclient login request. Turns out no matter how much I set: var cookieJar = NSHttpCookieStorage.SharedStorage; cookieJar.AcceptPolicy = NSHttpCookieAcceptPolicy.Always;

When debugging in the iphone simulator and running the webview is indicated in the browser that the cookie policy is not enabled, so the user cannot log in because the webview runs an iframe of a safe environment. Below is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Foundation;
using UIKit;
using Xamarin.Forms;
using Projeto.Custom;
using Projeto.iOS.Renderers;
using Xamarin.Forms.Platform.iOS;
using WebKit;
using System.IO;
using System.Net;

[assembly: ExportRenderer(typeof(CustomWebView), typeof(CustomWebViewRenderer))]
namespace Mynamespace.iOS.Renderers
{
    public class CustomWebViewRenderer : ViewRenderer<CustomWebView, WKWebView>
    {

        protected override void OnElementChanged(ElementChangedEventArgs<CustomWebView> e)
        {
            base.OnElementChanged(e);

            if(Control == null)
            {
                var userController = new WKUserContentController();
                var config = new WKWebViewConfiguration { 
                UserContentController = userController };
                var webView = new WKWebView(Frame, config);
                SetNativeControl(webView);
            }

            if(e.OldElement != null)
            {
                var hybrid = e.OldElement as CustomWebView;
                hybrid.Cleanup();
            }

            if(e.NewElement != null)
            {
                var baseUrl = new NSUrl(NSBundle.MainBundle.BundlePath, 
                true);
                string content = Element.Uri;
                Control.LoadHtmlString(content, baseUrl);              

                var cookieUrl = new 
                Uri("https://secure.gooddata.com/gdc/account/login");
                var cookieJar = NSHttpCookieStorage.SharedStorage;
                cookieJar.AcceptPolicy = NSHttpCookieAcceptPolicy.Always;
                foreach (var aCookie in cookieJar.Cookies)
                {
                    cookieJar.DeleteCookie(aCookie);
                }

                var jCookies = 
                CustomCookie.CookieContainer.GetCookies(cookieUrl);
                IList<NSHttpCookie> eCookies =
                    (from object jCookie in jCookies
                     where jCookie != null
                     select (Cookie)jCookie
                     into netCookie
                     select new NSHttpCookie(netCookie)).ToList();
                cookieJar.SetCookies(eCookies.ToArray(), cookieUrl, cookieUrl);

            }
        }
    }
}

If anyone can tell me the best way to activate the cookie policy in the native Webview of Ios, I would like to thank you in advance.

1 answer

2


Well after I broke my head, I was able to solve it by simply replacing Wkwebview with Uiwebview. What it seems is that Wkwebview is bugged to access Nshttpcookiestorage.Sharedstorage, as discussed here Behold

So just replace Wkwebview in Viewrenderer with Uiwebview and everything works perfectly. I noticed in the performance also Uiwebview proves to be faster, in my case I am loading an Htmlstring with an embedded iframe with dozens of graphics and Uiwebview, proved to be superior.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.