22

I'm using a cookie in my app which works fine in all browsers, but in android device the cookie is not setting as fast as I wanted, it takes some time until cookie is saved, same is happening when I delete the cookie. Is there anything I can do to make it work better? Thank in advance for your answers.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webview = new WebView(this);
    webview.getSettings().setJavaScriptEnabled(true); // enable javascript

    CookieManager.setAcceptFileSchemeCookies(true);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    cookieManager.acceptCookie();
    String cookie = CookieManager.getInstance().getCookie("mylink");

    final Activity activity = this;

    webview.setWebViewClient(new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
        }
    });
    webview.loadUrl("mylink");

    setContentView(webview);
}
Ziem
  • 6,579
  • 8
  • 53
  • 86
Ioana Ab.
  • 215
  • 1
  • 2
  • 5

3 Answers3

22

On Lollipop and beyond, the CookieManager singleton works fine by itself. (Refer Link - http://developer.android.com/reference/android/webkit/CookieManager.html) however, prior to Lollipop it also required the use of an additional static method from CookieSyncManager. The code below works for me on all Android versions when setting the cookies on a WebView -

CookieManager cookieManager = CookieManager.getInstance();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    CookieSyncManager.createInstance(this);
}
cookieManager.setAcceptCookie(true);
DarkKnight
  • 651
  • 5
  • 15
  • I would appreciate if you tell me how to save the cookies for reuse if app is closed(Make the cookie available for entire app ) ? I am loading a login page on webview and each time if app is close i have to re login! – user1788736 Dec 15 '17 at 19:58
  • 1
    Its not working for me..can you please suggest me any better way.. – Vishwa Pratap Feb 03 '20 at 12:21
6

Simply Just enable javascript And Dom Storage. this helps me to remember my login details in my webview android app. I Didn't use Any CookieManager But Enabling This Do the trick for me.

 webView.getSettings().setJavaScriptEnabled(true);
 webView.getSettings().setDomStorageEnabled(true);
Sharoon Ck
  • 738
  • 7
  • 14
4

Nothing : "The WebView now automatically syncs cookies as necessary. You no longer need to create or use the CookieSyncManager."

As DarkKnight said, you can test if your app target below API 21 Lollipop (5.0), if not, you don't need anymore CookieSyncManager.

Christian
  • 748
  • 7
  • 23
  • That's OK 8f you just want to be sure cookies are stores, but what if you want to give your users the option to, for example, delete cookies? Surely you need the `CookieManager` to do that, no? – Fat Monk Nov 16 '22 at 14:00