Xamarin.Forms ios WebViewRenderer sharing Cookies

0

I'm using a WebViewRenderer to setup the cookie policy and also to share cookies from a login request from an HTTPClient. It turns out that as much as I give the set:   var cookieJar = NSHttpCookieStorage.SharedStorage; cookieJar.AcceptPolicy = NSHttpCookieAcceptPolicy.Always;

When debugging the iphone simulator and running the webview, it is indicated in the browser that the cookies policy is not enabled, so the user can not log in because the webview runs an iframe from a secure 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 someone can tell me the best way to enable cookie policy in ios native WebView, thank you in advance.

    
asked by anonymous 23.04.2017 / 00:39

1 answer

2

Well after breaking my head, I managed to resolve it simply by replacing WKWebView with UIWebView. What it looks like is that WKWebView has a bug to access NSHttpCookieStorage.SharedStorage, as this discussion here See

So just replace WKWebView with UIWebView in the ViewRenderer and everything works perfectly. I noticed in the performance also the UIWebView proves to be faster, in my case I'm loading an HtmlString with an embedded iframe with dozens of graphs and UIWebView, has proven to be superior.

    
25.04.2017 / 17:37