0

I have an Android app which loads a website in a WebView. The website has cookies which store various information.

The cookies are created in the following way in C#:

Response.Cookies["city"].Expires = DateTime.MaxValue;

Problem:

When the website loads in the WebView it takes aprox. 30 seconds for the cookies to get saved onto the Android device.

If the user closes the application before ±30 seconds the cookies do not get stored on the Android device.

Question

How can I force and ensure that the cookies get saved to the Android device immediately when the website is opened in the webview?

What I've tried so far:

I've tried to implement the solution for this question but it did not help me. `$cookies.put()` takes 30 seconds to save the cookie on disk on Android

I also tried removing the part from my Android code where it disables the cache but the problem still persists.

This is my Android Code that I have:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mywebView = (WebView) findViewById(R.id.webView);
        mywebView.setBackgroundColor(Color.rgb(26,26,26));
        mywebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = mywebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAppCacheEnabled(false);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        mywebView.setWebChromeClient(new WebChromeClient());

        if (Build.VERSION.SDK_INT >= 21) {
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setAcceptThirdPartyCookies(mywebView, true);
            cookieManager.getInstance().flush();
        }

        mywebView.loadUrl("myURL");
        mywebView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                findViewById(R.id.progress1).setVisibility(View.GONE);
                findViewById(R.id.progressBarMessage).setVisibility(View.GONE);
            }
        });
    }

Please advise how I can solve this problem. Thank you in advance.

Edric
  • 24,639
  • 13
  • 81
  • 91

1 Answers1

0

I have edited my code by adding the following code and that seems solve the problem

            @Override
            public void onPageFinished(WebView view, String url){
                if (android.os.Build.VERSION.SDK_INT >= 21) {
                    CookieManager.getInstance().setAcceptThirdPartyCookies(view, true);
                    CookieManager.getInstance().flush();
                } else {
                    CookieManager.getInstance().setAcceptCookie(true);
                }
            }

If anyone has a better solution, please let me know.