6

I have been facing some problem with Old API versions. Some Links of some websites don't respond as they have preconditions that third party cookies must be enabled for webview. I did some search on topic and found an API :

CookieManager.getInstance().acceptThirdPartyCookies();

It fixes my issue and enables third party cookies but Min API level is 21. I need to support lower API level as low as 15. Is there any way we can perform same operation in lower API.

[Please note that API : CookieManager.getInstance().setAcceptCookie(true); is for enabling cookies and not third party cookies so it does not do the trick...:-(]

Tarun Deep Attri
  • 8,174
  • 8
  • 41
  • 56

2 Answers2

11

I found one answer feel like to share it. In Lower than LOLLIPOP(including LOLLIPOP) Third party cookies are enabled by default. In higher API levels than LOLLIPOP we need to explicitly set third party cookies so I added following if else in my code(Min API 16) :

    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
        Log.d(AppConstants.TAG,"SDk version above android L so forcibaly enabling ThirdPartyCookies");
        CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView,true);
    }

However it shows compile time error. it does not stop build and in higher API levels where we need to forcibly set third party cookies, this code does the trick.

Tarun Deep Attri
  • 8,174
  • 8
  • 41
  • 56
  • setAcceptThirdPartyCookies() requires at least api 21(LOLLIPOP), so it should be :if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) . According to https://developer.android.com/reference/android/webkit/CookieManager.html#setAcceptThirdPartyCookies(android.webkit.WebView,%20boolean) – thetaprime Apr 08 '18 at 03:36
0

CookieManager.getInstance() is the CookieManager instance for your entire application. Hence, you enable or disable cookies for all the webviews in your application.

Maybe you call CookieManager.getInstance().setAcceptCookie(true);

OR

Please try this method, before a WebView is created or CookieManager accessed: http://developer.android.com/reference/android/webkit/CookieManager.html#setAcceptFileSchemeCookies(boolean)

Please read the documentation for this method!

Community
  • 1
  • 1
Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50