C# 表单ios WebViewRenderer共享Cookies

C# 表单ios WebViewRenderer共享Cookies,c#,uiwebview,xamarin.ios,C#,Uiwebview,Xamarin.ios,我正在使用WebViewRenderer来设置cookie策略,并共享来自HTTPClient的登录请求的cookie。事实证明,尽管我给了这个场景: var cookieJar=NSHttpCookieStorage.SharedStorage; cookieJar.AcceptPolicy=NSHttpCookieAcceptPolicy.Always 在iphone模拟器中调试并运行webview时,浏览器中会指示Cookie策略未启用,因此用户无法登录,因为webview从安全环境运行i

我正在使用WebViewRenderer来设置cookie策略,并共享来自HTTPClient的登录请求的cookie。事实证明,尽管我给了这个场景:
var cookieJar=NSHttpCookieStorage.SharedStorage;
cookieJar.AcceptPolicy=NSHttpCookieAcceptPolicy.Always

在iphone模拟器中调试并运行webview时,浏览器中会指示Cookie策略未启用,因此用户无法登录,因为webview从安全环境运行iframe。代码如下:

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

            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用基础;
使用UIKit;
使用Xamarin.Forms;
使用Projeto.Custom;
使用Projeto.iOS.renders;
使用Xamarin.Forms.Platform.iOS;
使用WebKit;
使用System.IO;
Net系统;
[程序集:ExportRenderer(typeof(CustomWebView)、typeof(CustomWebViewRenderer))]
命名空间Mynamespace.iOS.Renderers
{
公共类CustomWebViewRenderer:ViewRenderer
{
受保护的覆盖无效OnElementChanged(ElementChangedEventArgs e)
{
基础。一个要素发生变化(e);
if(Control==null)
{
var userController=new WKUserContentController();
var config=新的WKWebViewConfiguration{
UserContentController=userController};
var webView=新的WKWebView(框架,配置);
SetNativeControl(网络视图);
}
if(e.OldElement!=null)
{
var hybrid=e.OldElement作为CustomWebView;
hybrid.Cleanup();
}
if(例如NewElement!=null)
{
var baseUrl=新NSUrl(NSBundle.MainBundle.BundlePath,
正确的);
字符串内容=Element.Uri;
Control.LoadHtmlString(内容,baseUrl);
var cookieUrl=new
Uri(“https://secure.gooddata.com/gdc/account/login");
var cookieJar=NSHttpCookieStorage.SharedStorage;
cookieJar.AcceptPolicy=NSHttpCookieAcceptPolicy.Always;
foreach(cookieJar.Cookies中的var aCookie)
{
删除cookie(aCookie);
}
var jCookies=
CustomCookie.CookieContainer.GetCookies(cookieUrl);
伊利斯特生态酒店=
(来自jCookies中的对象jCookie)
其中jCookie!=null
选择(Cookie)jCookie
进入netCookie
选择新的NSHttpCookie(netCookie)).ToList();
SetCookies(eCookies.ToArray(),cookieUrl,cookieUrl);
}
}
}
}
如果有人能告诉我在ios原生WebView中启用cookie策略的最佳方法,我将不胜感激