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.