Enable Third party cookies for Android WebView

10,757

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.

Share:
10,757
Tarun Deep Attri
Author by

Tarun Deep Attri

I am a JAVA and ANDROID enthusiast who is trying to explore everything about these fields and learn more and more and also spread more and more knowledge in the process. #SOreadytohelp

Updated on June 15, 2022

Comments

  • Tarun Deep Attri
    Tarun Deep Attri almost 2 years

    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...:-(]