Problem with cookies in UIWebView

StrathmoreStrathmore USMember
edited February 2017 in Xamarin.iOS

I used NSHttpCookieStorage.SharedStorage, property NSHttpCookieStorage.SharedStorage.AcceptPolicy is NSHttpCookieAcceptPolicy.Always.
Page that loads to webview does not show the cookies that stores the ios app - before load website.

My stored function:

NSHttpCookieStorage storage = NSHttpCookieStorage.SharedStorage;
Cookie registerCookie = new Cookie();
registerCookie.Domain = Constants.baseWebUrl;
registerCookie.Name = "name";
registerCookie.Value = "testname";
registerCookie.Expires = DateTime.Now.AddDays(1);
var myCookie = new NSHttpCookie(registerCookie);
storage.SetCookie(myCookie);

But when I read SharedStorage:

 public NSHttpCookie[] ReadCookies()
        {
            foreach (var aCookie in storage.Cookies)
            {
                Console.WriteLine(aCookie.Name);
            }
            return storage.Cookies;
 }

then shows all cookies (include cookies generated by web page).
Page in the browser does not detect cookies added by the above functions.

Thanks in advance.

Posts

  • MohammadShabanMohammadShaban USMember ✭✭

    same for me, did you find a solution?

  • BsaferBsafer USMember ✭✭

    I spent way to much time on this and finally figured out how to set the NSHttpCookieStorage.SharedStorage before showing the UIWebView in a viewrenderer. The key was the Path vs Domain setting. I looked at the sharedstorage to see how the webview was storing the cookies, and noticed the Path wasn't what I thought it was nor the domain. Once I changed to match what they had, it worked.

                       NSHttpCookieStorage.SharedStorage.AcceptPolicy = NSHttpCookieAcceptPolicy.Always;
                        thisPCOUri = new Uri(Element.Uri);
                        string thisDomain = ".mydomain.com";
                        //set starting cookies.  First delete all for this site.
                        NSHttpCookie[] startingcookies = NSHttpCookieStorage.SharedStorage.CookiesForUrl(thisPCOUri);
                        foreach (NSHttpCookie cookie in startingcookies)
                        {
                            NSHttpCookieStorage.SharedStorage.DeleteCookie(cookie);
                        }
                        //  now add back in cookies
                        NSHttpCookie[] newccs = new NSHttpCookie[MySettings.PCOCookie.Count];
                        foreach (KeyValuePair<string, string> curcook in MySettings.PCOCookie)
                        {
                            NSHttpCookie cc = new NSHttpCookie(curcook.Key, curcook.Value, "/", thisDomain);
                            NSHttpCookieStorage.SharedStorage.SetCookie(cc);
                        }
                        NSHttpCookie[] startingcookiesafter = NSHttpCookieStorage.SharedStorage.CookiesForUrl(thisPCOUri);
    

    Hope this helps the next person.
    Brent

  • EasyEddyEasyEddy USMember ✭✭

    Thank you BrentSafer, saved me a lot of time ;)

    // EDDY

  • MySettings.PCOCookie what this class?

Sign In or Register to comment.