3

I am creating an android application in xamarin. I am using a web view to display a website. After user login website create a cookie and should store in web view. There are two web view in app, One web view is displaying the pages and if there is any text box in the page that page is opened in second web view.

So now when user tries to login, second web view is opened(as login page contains text box), after user enter details and click next button, the second view is closed and next page is opened in first web view. After login a cookie is created and stored in web view and when user open the app next time its doesn't asks for login. This is what should happen.

The problem is, if the user enter details and after clicks the next button(next page is loading in first web view) and immediately quits the application then start app again then cookies does not exists and app asks for login again.

After login I am reading the cookie value on page finish event of webview and displaying in toast. If i quits the app after login, i gets the cookie value in toast but when i starts the app again the cookie doesn't exists anymore and it asks me for login again

public override void OnPageFinished (WebView view, string url)
        {
            try 
            {
                if (view.Url == Urls.URL_INDEX) 
                {
                    var cookieManager = CookieManager.Instance;
                    if (cookieManager != null) 
                    {
                        //getcookie string from the url
                        string cookie = cookieManager.GetCookie (view.Url);
                        if (!string.IsNullOrEmpty (cookie)) 
                        {
                            string[] cookies = cookie.Split (';');
                            foreach (var newcookie in cookies) 
                            {
                                if (newcookie.Trim().StartsWith (Constants.COOKIE_NAME)) 
                                {
                                    string cookieValue = newcookie.Substring (newcookie.IndexOf ('='));             
                                    Toast.MakeText(activity,cookieValue,ToastLength.Short).Show();
                                }
                            }
                        }
                    } 
                }
            }
            catch (Exception ex) 
            {
                Console.WriteLine ("Exception in storing cookie in Home Activity : "+ex.Message);
                Toast.MakeText (activity, "Exception : " + ex.Message,ToastLength.Long).Show();
            }
        }

I don't know why this is happening, please help.

Lalit Kumar
  • 146
  • 1
  • 6

1 Answers1

2

The cookies are stored in RAM to get the best performance and synced every five minutes to the permanent storage. You'll need to manually force CookieSyncManager to sync the cookies in your OnPageFinished method so that they're still available when you start the application again. Refer to the CookieSyncManager documentation for more details.

Timo Salomäki
  • 7,099
  • 3
  • 25
  • 40
  • Thanks that worked, But CookieSyncManager class is Obsoleted, what should i use instead of it – Lalit Kumar Aug 03 '16 at 06:46
  • Great to hear. Feel free to mark the answer as correct if it solved your problem. Regarding the second question, [this thread](http://stackoverflow.com/questions/30502411/cookiesyncmanager-is-now-deprecated-what-can-i-use-instead) will give you an answer. – Timo Salomäki Aug 03 '16 at 20:44