Forum Xamarin Xamarin.iOS

How to Copy Cookie to Webview in iOS?

ErdoganErdogan Member ✭✭✭
edited January 14 in Xamarin.iOS

How can I copy a third party cookie to Webview?

It is very easy in Android, just with a single code line in custom renderer;
CookieManager.Instance.SetAcceptThirdPartyCookies(Control, true);

After a few days of research, I failed and very confused. There are some solutions in the forum and web but I guess there is no updated answer.

What is the best practice to do it for iOS >= 11.0?

My webview

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

Custom Renderer

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

            }

Answers

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    Different from UIWebView we nee to add the cookie manually

    in custom renderer

    NSString cookieStr = new NSString("xxx"); 
    WKUserScript userScript = new WKUserScript(cookieStr, WKUserScriptInjectionTime.AtDocumentStart,false);
    WKUserContentController userContentController = new WKUserContentController();
    userContentController.AddUserScript(userScript);
    WKWebViewConfiguration configuration = new WKWebViewConfiguration();
    configuration.UserContentController = userContentController;
    
     webView = new WKWebView(Frame, configuration);
    SetNativeControl(webView);
    
    
  • ErdoganErdogan Member ✭✭✭

    Thank you @LucasZhang

    But It doesn't work on the simulator, do you think it's related to the simulator?

    And maybe it's about my wrong usage, I don't know because this renderer fires one time.

    according to my scenario;

    NSString cookieStr = new NSString("MyTargetWebsite.com"); // I want to set this website's cookies to my webview

    But my webview's home page is different. After a click on the landing page, it will redirect to my target website.

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

    How can I set the cookies to my Webview after a redirect, or is it enough to set it up from the beginning just like your solution?

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    cookieStr is the cookie like document.cookie = 'user=%@';

  • ErdoganErdogan Member ✭✭✭
    edited January 14

    @Lucas

    Still, I don't understand, sorry.

    Shouldn't I get a cookie list from the visited page?
    Shouldn't necessary an event per request? This method (OnElementChanged) fires once in the first request.
    Every website has its private cookies. Target websites are not on mine. Because of that, your solution is appropriate for my problem?

    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.

  • LucasZhangLucasZhang Member, Xamarin Team Xamurai

    My code is just a demo . You should use your cookie instead of the string .

  • ErdoganErdogan Member ✭✭✭

    Thanks, Lucas, but as in my question title, "how to copy cookies", I don't know what way correct is.

    Shouldn't it necessary an event to get the current page's cookies per request?

Sign In or Register to comment.