如何在iOS中将Cookie复制到WKWebview? [Xamarin]

问题描述 投票:0回答:1

我不知道来自哪个网站的cookie是怎么来的。因此,我无法手动设置cookie名称。

如何获得第三方Cookie粘贴到WKWebview?这是我的代码,但没有机会。

我的网络视图;

public class CustomWebView : WebView
{
    public static readonly BindableProperty UriProperty =
        BindableProperty.Create(
            propertyName: "Uri",
            returnType: typeof(Uri),
            declaringType: typeof(CustomWebView),
            defaultValue: default(string));

    public string Uri
    {
        get { return (string)GetValue(UriProperty); }
        set { SetValue(UriProperty, value); }
    }
}

我的自定义渲染器(每个请求都不需要一个事件吗?此方法在第一个请求中触发一次);

[assembly: ExportRenderer(typeof(CustomWebView), typeof(HTMobile.iOS.WebViewRenderer))]
namespace HTMobile.iOS
{
    public class WebViewRenderer : ViewRenderer<CustomWebView, WKWebView>
    {
        protected override void OnElementChanged(ElementChangedEventArgs<CustomWebView> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
                // Cookie
                var cookieUrl = new Uri("abc.com"); 
                NSHttpCookieStorage.SharedStorage.AcceptPolicy = NSHttpCookieAcceptPolicy.Always;
                var cookieJar = NSHttpCookieStorage.SharedStorage;
                cookieJar.AcceptPolicy = NSHttpCookieAcceptPolicy.Always;
                foreach (var aCookie in cookieJar.Cookies)
                {
                    cookieJar.DeleteCookie(aCookie);
                }

                var jCookies = UserInfo.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);

                // WebView Instance
                webView = new WKWebView(Frame, new WKWebViewConfiguration());
                SetNativeControl(webView);

                if (e.NewElement != null)
                {
                    Control.LoadRequest(new NSUrlRequest(new NSUrl("abc.com")));
                }
            }
        }
    }
}

我认为应该为每个请求触发一个事件,我应该能够为访问的页面获取cookie列表,然后将其设置为我的WebView

请咨询。

cookies xamarin.forms wkwebview
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.