Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable Third party cookies for Android WebView

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

like image 201
Tarun Deep Attri Avatar asked Feb 04 '16 11:02

Tarun Deep Attri


1 Answers

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.

like image 169
Tarun Deep Attri Avatar answered Sep 22 '22 01:09

Tarun Deep Attri