Enable third-party cookies for Android WebView

I encountered some problems when using the old API version. Some links of some websites did not respond because they had the premise that third-party cookies must be enabled for WebView. I searched the topic and found an API:

CookieManager.getInstance().acceptThirdPartyCookies();

It can solve my problem and enable third-party cookies, but the minimum API level is 21. I need to support lower API levels as low as 15. What methods can perform the same operation in lower APIs

[please note that API: Cookie manager. Getinstance(). Setacceptcookie (true); it is used to enable Cookies rather than third-party cookies, so it doesn't work...: - (]

resolvent:

I found an answer I want to share. Third party cookies are enabled by default when it is lower than lollipop (including lollipop). In API levels higher than lollipop, we need to explicitly set third-party cookies, so I added the following content to the code (minimum 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 errors. It does not stop building, and this code can solve the problem at a higher API level where we need to force third-party cookies

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>