如何在iOS中将Cookie复制到WKWebview? [英] How to Copy Cookie to WKWebview in iOS?

查看:121
本文介绍了如何在iOS中将Cookie复制到WKWebview?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I don't know how/which cookies are coming from which website. Because of that, I can't set manually the cookie names.

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

How can I get third party cookies to paste to a WKWebview? Here is my code but no chance.

我的网络视图;

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

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

My custom renderer (Shouldn't necessary an event per request? This method fires once in the first request);

[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.

I think an event should be fired per request and I should be able to get a cookie list for the visited page and then set it to my WebView.

请提出建议.

推荐答案

您可以尝试通过调用

You can have a try with getting cookie by invoking DecidePolicy method from WKNavigationDelegate .

public class NavigationDelegate : WKNavigationDelegate
{
    NSMutableArray multiCookieArr = new NSMutableArray();

public override void DecidePolicy(WKWebView webView, WKNavigationResponse navigationResponse, [BlockProxy(typeof(Action))]Action<WKNavigationResponsePolicy> decisionHandler)
{

    if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
    {
        WKHttpCookieStore wKHttpCookieStore = webView.Configuration.WebsiteDataStore.HttpCookieStore;
        Console.WriteLine("wKHttpCookieStore is :" + wKHttpCookieStore.GetDebugDescription());
        wKHttpCookieStore.GetAllCookies(cookies => {
            if(cookies.Length > 0)
            {
                foreach (NSHttpCookie cookie in cookies)
                {
                    //NSHttpCookieStorage.SharedStorage.SetCookie(cookie);
                    Console.WriteLine("cookie is :" + cookie);
                }

            }
        });
    }
    else
    {
        NSHttpUrlResponse response = navigationResponse.Response as NSHttpUrlResponse;
        NSHttpCookie[] cookiesAll = NSHttpCookie.CookiesWithResponseHeaderFields(response.AllHeaderFields, response.Url);
        foreach (NSHttpCookie cookie in cookiesAll)
        {
            Console.WriteLine("Here is the cookie inside wkwebview is :" + cookie);
            NSArray cookieArr = NSArray.FromObjects(cookie.Name, cookie.Value, cookie.Domain, cookie.Path);
            multiCookieArr.Add(cookieArr);
        }
        Console.WriteLine("cookie is :" + cookiesAll);
    }

    decisionHandler(WKNavigationResponsePolicy.Allow);

    //base.DecidePolicy(webView, navigationResponse, decisionHandler);
}

此外,使用Renderer来自定义WebView ,您可以参考此文档.

In addition , using Renderer to Customizing a WebView you can refer to this doc .

public class HybridWebViewRenderer : WkWebViewRenderer
{

    public HybridWebViewRenderer() : this(new WKWebViewConfiguration())
    {
    }

    public HybridWebViewRenderer(WKWebViewConfiguration config) : base(config)
    {   
    }

    protected override void OnElementChanged(VisualElementChangedEventArgs e)
    {
        base.OnElementChanged(e);

        if (e.OldElement != null)
        {
          //...
        }

        if (e.NewElement != null)
        {
            this.NavigationDelegate = new NavigationDelegat();
        }
    }
}

这篇关于如何在iOS中将Cookie复制到WKWebview?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆